Open History to see the last commit
1---
2id: SortedMap
3title: SortedMap
4---
5
6Defined in: [packages/db/src/SortedMap.ts:8](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L8)
7
8A Map implementation that keeps its entries sorted based on a comparator function
9
10## Type Parameters
11
12### TKey
13
14`TKey` *extends* `string` \| `number`
15
16The type of keys in the map (must be string | number)
17
18### TValue
19
20`TValue`
21
22The type of values in the map
23
24## Constructors
25
26### Constructor
27
28```ts
29new SortedMap<TKey, TValue>(comparator?): SortedMap<TKey, TValue>;
30```
31
32Defined in: [packages/db/src/SortedMap.ts:21](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L21)
33
34Creates a new SortedMap instance
35
36#### Parameters
37
38##### comparator?
39
40(`a`, `b`) => `number`
41
42Optional function to compare values for sorting.
43 If not provided, entries are sorted by key only.
44
45#### Returns
46
47`SortedMap`\<`TKey`, `TValue`\>
48
49## Accessors
50
51### size
52
53#### Get Signature
54
55```ts
56get size(): number;
57```
58
59Defined in: [packages/db/src/SortedMap.ts:201](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L201)
60
61Gets the number of key-value pairs in the map
62
63##### Returns
64
65`number`
66
67## Methods
68
69### \[iterator\]()
70
71```ts
72iterator: IterableIterator<[TKey, TValue]>;
73```
74
75Defined in: [packages/db/src/SortedMap.ts:210](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L210)
76
77Default iterator that returns entries in sorted order
78
79#### Returns
80
81`IterableIterator`\<\[`TKey`, `TValue`\]\>
82
83An iterator for the map's entries
84
85***
86
87### clear()
88
89```ts
90clear(): void;
91```
92
93Defined in: [packages/db/src/SortedMap.ts:191](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L191)
94
95Removes all key-value pairs from the map
96
97#### Returns
98
99`void`
100
101***
102
103### delete()
104
105```ts
106delete(key, deferOrder): boolean;
107```
108
109Defined in: [packages/db/src/SortedMap.ts:158](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L158)
110
111Removes a key-value pair from the map
112
113#### Parameters
114
115##### key
116
117`TKey`
118
119The key to remove
120
121##### deferOrder
122
123`boolean` = `false`
124
125Defer ordering until restoreOrder or the next ordered read
126
127#### Returns
128
129`boolean`
130
131True if the key was found and removed, false otherwise
132
133***
134
135### entries()
136
137```ts
138entries(): IterableIterator<[TKey, TValue]>;
139```
140
141Defined in: [packages/db/src/SortedMap.ts:222](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L222)
142
143Returns an iterator for the map's entries in sorted order
144
145#### Returns
146
147`IterableIterator`\<\[`TKey`, `TValue`\]\>
148
149An iterator for the map's entries
150
151***
152
153### forEach()
154
155```ts
156forEach(callbackfn): void;
157```
158
159Defined in: [packages/db/src/SortedMap.ts:253](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L253)
160
161Executes a callback function for each key-value pair in the map in sorted order
162
163#### Parameters
164
165##### callbackfn
166
167(`value`, `key`, `map`) => `void`
168
169Function to execute for each entry
170
171#### Returns
172
173`void`
174
175***
176
177### get()
178
179```ts
180get(key): TValue | undefined;
181```
182
183Defined in: [packages/db/src/SortedMap.ts:147](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L147)
184
185Gets a value by its key
186
187#### Parameters
188
189##### key
190
191`TKey`
192
193The key to look up
194
195#### Returns
196
197`TValue` \| `undefined`
198
199The value associated with the key, or undefined if not found
200
201***
202
203### has()
204
205```ts
206has(key): boolean;
207```
208
209Defined in: [packages/db/src/SortedMap.ts:184](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L184)
210
211Checks if a key exists in the map
212
213#### Parameters
214
215##### key
216
217`TKey`
218
219The key to check
220
221#### Returns
222
223`boolean`
224
225True if the key exists, false otherwise
226
227***
228
229### keys()
230
231```ts
232keys(): IterableIterator<TKey>;
233```
234
235Defined in: [packages/db/src/SortedMap.ts:231](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L231)
236
237Returns an iterator for the map's keys in sorted order
238
239#### Returns
240
241`IterableIterator`\<`TKey`\>
242
243An iterator for the map's keys
244
245***
246
247### restoreOrder()
248
249```ts
250restoreOrder(): void;
251```
252
253Defined in: [packages/db/src/SortedMap.ts:88](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L88)
254
255Restore the ordered view after a synchronous batch of deferred writes.
256
257#### Returns
258
259`void`
260
261***
262
263### set()
264
265```ts
266set(
267 key,
268 value,
269 deferOrder): this;
270```
271
272Defined in: [packages/db/src/SortedMap.ts:108](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L108)
273
274Sets a key-value pair in the map and maintains sort order
275
276#### Parameters
277
278##### key
279
280`TKey`
281
282The key to set
283
284##### value
285
286`TValue`
287
288The value to associate with the key
289
290##### deferOrder
291
292`boolean` = `false`
293
294Defer ordering until restoreOrder or the next ordered read
295
296#### Returns
297
298`this`
299
300This SortedMap instance for chaining
301
302***
303
304### values()
305
306```ts
307values(): IterableIterator<TValue>;
308```
309
310Defined in: [packages/db/src/SortedMap.ts:241](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L241)
311
312Returns an iterator for the map's values in sorted order
313
314#### Returns
315
316`IterableIterator`\<`TValue`\>
317
318An iterator for the map's values