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