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:19](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L19)
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:157](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L157)
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:166](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L166)
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:149](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L149)
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): boolean;
107```
108
109Defined in: [packages/db/src/SortedMap.ts:125](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L125)
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#### Returns
122
123`boolean`
124
125True if the key was found and removed, false otherwise
126
127***
128
129### entries()
130
131```ts
132entries(): IterableIterator<[TKey, TValue]>;
133```
134
135Defined in: [packages/db/src/SortedMap.ts:177](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L177)
136
137Returns an iterator for the map's entries in sorted order
138
139#### Returns
140
141`IterableIterator`\<\[`TKey`, `TValue`\]\>
142
143An iterator for the map's entries
144
145***
146
147### forEach()
148
149```ts
150forEach(callbackfn): void;
151```
152
153Defined in: [packages/db/src/SortedMap.ts:208](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L208)
154
155Executes a callback function for each key-value pair in the map in sorted order
156
157#### Parameters
158
159##### callbackfn
160
161(`value`, `key`, `map`) => `void`
162
163Function to execute for each entry
164
165#### Returns
166
167`void`
168
169***
170
171### get()
172
173```ts
174get(key): TValue | undefined;
175```
176
177Defined in: [packages/db/src/SortedMap.ts:115](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L115)
178
179Gets a value by its key
180
181#### Parameters
182
183##### key
184
185`TKey`
186
187The key to look up
188
189#### Returns
190
191`TValue` \| `undefined`
192
193The value associated with the key, or undefined if not found
194
195***
196
197### has()
198
199```ts
200has(key): boolean;
201```
202
203Defined in: [packages/db/src/SortedMap.ts:142](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L142)
204
205Checks if a key exists in the map
206
207#### Parameters
208
209##### key
210
211`TKey`
212
213The key to check
214
215#### Returns
216
217`boolean`
218
219True if the key exists, false otherwise
220
221***
222
223### keys()
224
225```ts
226keys(): IterableIterator<TKey>;
227```
228
229Defined in: [packages/db/src/SortedMap.ts:186](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L186)
230
231Returns an iterator for the map's keys in sorted order
232
233#### Returns
234
235`IterableIterator`\<`TKey`\>
236
237An iterator for the map's keys
238
239***
240
241### set()
242
243```ts
244set(key, value): this;
245```
246
247Defined in: [packages/db/src/SortedMap.ts:92](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L92)
248
249Sets a key-value pair in the map and maintains sort order
250
251#### Parameters
252
253##### key
254
255`TKey`
256
257The key to set
258
259##### value
260
261`TValue`
262
263The value to associate with the key
264
265#### Returns
266
267`this`
268
269This SortedMap instance for chaining
270
271***
272
273### values()
274
275```ts
276values(): IterableIterator<TValue>;
277```
278
279Defined in: [packages/db/src/SortedMap.ts:195](https://github.com/TanStack/db/blob/main/packages/db/src/SortedMap.ts#L195)
280
281Returns an iterator for the map's values in sorted order
282
283#### Returns
284
285`IterableIterator`\<`TValue`\>
286
287An iterator for the map's values