Comments (3)
Hey, yep you're missing something :-)
The sorted map is self-sorting - that is, you define the sorting criteria when you construct the map, and it sorts items according to those criteria. If you could just insert items into any index, it wouldn't necessarily be sorted. If you don't specify any sorting criteria, the items are sorted in order-of-insertion.
You have two options:
-
You could use a list and a map; use the map to store an index, then insert the item into the associated index in the list.
-
Store the index as a field in the object you're adding to the sorted map, then specify the index field as the sorting criteria, like so:
import { empty, set } from '@collectable/sorted-map';
const comparator = (a, b) => a.value.index - b.value.index;
const sortedMap = empty(comparator);
const updatedMap = set('key', { id: 1, index: 10 }, sortedMap);
To change the index, just remove the item and re-insert it.
from collectable.
No worries! Also, note that you'll experience unexpected behaviour if you mutate any fields that are used as sorting criteria before removing the associated object from the map. If you intend to be able to modify your object without having to remove it from the map first, I'd suggest using a wrapper object, e.g.:
const objectToInsert = { index: 10, value: myObject }
from collectable.
Somehow, I expected that, even though the map uses order-of-insertion by the default, I could still insert a value anywhere in the tree without too much hassle. Tbh I did not check the implementation details :) Option no 1 I had already considered, no 2 it's an approach I hadn't thought of, I'll check how it works. Thanks for the detailed answer!
from collectable.
Related Issues (20)
- batch.owner(true) should prefer -1 over current owner value
- Removing the last element from a mutable red-black tree results in incorrect behavior HOT 1
- Removing an element from a red-black tree sometimes modifies the remaining key-value pairs HOT 21
- Removing elements from a mutable red-black tree affects its immutable clones HOT 2
- fantasy-land compliance HOT 26
- Switch build package management from Yarn to NPM 5 HOT 3
- Travis unable to build for unknown reasons
- setIn behavior contradicts documentation HOT 17
- List.remove behaving weirdly HOT 7
- If yarn is preferred, why not commit the yarn.lock file? HOT 4
- setIn() for numerical indexes fails on empty lists HOT 7
- setIn() second parameter is treated as a value HOT 2
- feature request: Implement CHAMP for increased performance. HOT 3
- For Each HashMap Item HOT 6
- flowtype definitions HOT 1
- Question: is the built-in hash function sufficient? HOT 1
- NPM Failing on @collectable/* references and @frptools/* references HOT 5
- Frpbypass
- Maintenance status of this lib HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from collectable.