-
Notifications
You must be signed in to change notification settings - Fork 8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
What about #sort ? #12
Comments
|
I can see the value in determining iteration order - masking insertion order, for example, or having a Set subclass that maintains a sorting order. |
One of the use cases is when you add element, but want to preserve order of previous one (say by having "position" property on each object in the set). Another is just plain alphanumeric sorted abstract Again.. this discussion seems to be running in circles and i think that the issue with need for Anyway, lets see examples (with inline comments). Conversion to JSON: // lets transfer an object with .id and .relatedItems being set of unique ids
const myDTO = {
id: 42,
relatedItems: new Set(1,4,2,3,5,5,4)
};
// now i just want a sorted list of numbers converted to array and then to string
let notSoFast = JSON.stringify(myDTO); // obviously does not work
let butJsonHasStringifierArg = JSON.stringify(myDTO, ' ', function (k, v) {
if (v.toString === '[object Set]') {
// at this point, do i actually care for difference between
return [...v].sort().toString(); // wait - did i just killed my numbers ??
// or supposedly easier
return v.join(); // which does not sort (and kills numbers)
// and so what i really want is
return [...v].sort();
// which (even if .sort is added) could NOT be simplified to
return v.sort(); // because v will be ruined anyway
}
}); Another one, order. const s1 = new Set(1,2,3,5); // ordered
s1.delete(2); // still ordered
s1.add(2); // oops
const s2 = new Set([2,3,5]); // ordered
s2.add(1); // oops
let sorted1 = new Set([...s1.add(4)].sort()); // current syntax
let sorted2 = s2.add(4).sort(); // bright future strawman P.S.: i don't know the implementation details, and maybe all Sets in JS are actually something like java LinkedHashSet (and that's why they only maintain insertion order). If adding |
I am working on an HTTP router where "static" routes (i.e. ones where the path is a simple string with no parameters) are stored in a However, I'd like to make it so that when external tooling accesses the routing table to display it via a webpage, for example, then the table should be in a specific order for readability. In other words, I want the display order to be independent of the insertion order. It would be much more ergonomic to sort the |
Looks like many developers needs a way to sort a
Given that a
We could add: Set.prototype.sort(compareFn) With optional Of course Unoptimized workaround could be: const sortedSet = new Set(Array.from(set).sort()); My use case if for orderer unique items. See also:
PS: I've created an issue on tc39/proposal-set-methods but I think it fits better here. |
As per previous discussions, Set and Map have the inherrent order (insertion order).
Its possible to control it at creation time (by sorting initial iterable), but its not easy to change the order afterwards.
This proposal does not mention
sort
for neither of Map or Set, - but maybe this is a valid operation for both collection ?If this has been discussed before - feel free to close this issue
The text was updated successfully, but these errors were encountered: