Giter Site home page Giter Site logo

Comments (3)

axefrog avatar axefrog commented on May 25, 2024 1

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:

  1. 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.

  2. 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.

axefrog avatar axefrog commented on May 25, 2024 1

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.

tokland avatar tokland commented on May 25, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.