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