How can I sort an Set in JS

 

How can I sort an Set in JS

 

A set is not an ordered abstract data structure.

However, the set always has the same iteration order - element insertion order [1], so when you iterate over it (via an iterate method, calling Symbol.iterator or a for.. of loop), you can always expect that.

You can always convert a set to arrays and sort them.

here are following two examples 

 

Array.from(new Set([2,1,3])).sort();

 

[...(new Set([2,1,3]))].sort();

Tags:

Share:

Related posts