-1

I have a Map of object values. I want to sort them for a property of its object values.

Here is the sample Map:

var map = new Map();
map.set("8014",{
            "Access to support": null,
            "Capacity":  1,
            "Comfort Level": null,
            "Control Level": 1,
            "Growth Mindset": 1.5405405405405403,
            "Horizon": null,
            "Motivation": null,
            "Open-Mindedness": null,
            "Proactivity": null,
            "Resiliency": null,
            "Segment": "Level value 1"});
map.set("8015",{
            "Access to support": null,
            "Capacity":  1,
            "Comfort Level": null,
            "Control Level": 1,
            "Growth Mindset": 1.5405405405405403,
            "Horizon": null,
            "Motivation": null,
            "Open-Mindedness": null,
            "Proactivity": null,
            "Resiliency": null,
            "Segment": "Level value 2 or 5"});
map.set("8016",{
            "Access to support": null,
            "Capacity":  1,
            "Comfort Level": null,
            "Control Level": 1,
            "Growth Mindset": 1.5405405405405403,
            "Horizon": null,
            "Motivation": null,
            "Open-Mindedness": null,
            "Proactivity": null,
            "Resiliency": null,
            "Segment": "Level value 3 or 5"});

I want to sort it for Segment property of value object. Any help will be appreciated!

DevLoverUmar
  • 11,809
  • 11
  • 68
  • 98
  • 1
    Please create a [mcve] with a valid input, expected output with a clear problem statement. – adiga Jul 30 '19 at 07:43
  • 1
    is it [`Map`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Map) or just an array? why sorting? – Nina Scholz Jul 30 '19 at 07:43
  • Your object is invalid. Also, object keys cannot be sorted. Even if you do sort them, they are not guaranteed to remain in that order. – Kobe Jul 30 '19 at 07:43
  • @CertainPerformance, please take a look at the edited version. – DevLoverUmar Jul 30 '19 at 07:47
  • 1
    If that is a Map, that doesn't look valid. And what's the output - and type of output - meant to be? – Jack Bashford Jul 30 '19 at 07:50
  • @JackBashford I'm sorry for inconvenience. Please take a look at updated version. Its a map, I just updated to clearly describe my problem – DevLoverUmar Jul 30 '19 at 08:00
  • 1
    Maps are ordered (to have predictable iteration), but not sortable. [You need to sort an array](https://stackoverflow.com/q/50069004/1048572) – Bergi Jul 30 '19 at 08:04
  • 1
    If an answer resolved your issue, please [mark it as accepted](https://meta.stackexchange.com/a/5235/289255) by clicking on the grey checkmark next it. (I'm only mentioning it because you have not accepted any answer till date) – adiga Jul 30 '19 at 08:30
  • @adiga I have accepted your answer 20 minutes ago! – DevLoverUmar Jul 30 '19 at 08:38
  • 1
    To accept the answer, you have to click on the grey tick mark button next to the answer: https://i.stack.imgur.com/LkiIZ.png – adiga Jul 30 '19 at 08:42
  • 1
    You can also check your [previous questions](https://stackoverflow.com/users/7344164/muhammadumarfarooq?tab=questions) and accept the answers if they are resolved too (You get 2 reputation for accepting the answer) – adiga Jul 30 '19 at 09:07

2 Answers2

2

You could sort the entries of Map based on Segment by using localeCompare. Then create a new Map from the sorted entries

var map=new Map();map.set("8014",{"Access to support":null,Capacity:1,"Comfort Level":null,"Control Level":1,"Growth Mindset":1.5405405405405403,Horizon:null,Motivation:null,"Open-Mindedness":null,Proactivity:null,Resiliency:null,Segment:"Level value 1"});map.set("8015",{"Access to support":null,Capacity:1,"Comfort Level":null,"Control Level":1,"Growth Mindset":1.5405405405405403,Horizon:null,Motivation:null,"Open-Mindedness":null,Proactivity:null,Resiliency:null,Segment:"Level value 2 or 5"});map.set("8016",{"Access to support":null,Capacity:1,"Comfort Level":null,"Control Level":1,"Growth Mindset":1.5405405405405403,Horizon:null,Motivation:null,"Open-Mindedness":null,Proactivity:null,Resiliency:null,Segment:"Level value 3 or 5"});

const sortedMap =
  new Map(
    Array.from(map)
        .sort((a, b) => a[1].Segment.localeCompare(b[1].Segment))
  );

console.log([...sortedMap.entries()])
adiga
  • 34,372
  • 9
  • 61
  • 83
1

You need to rebuild the Map after sorting the items with a custom callback, because

The Map object holds key-value pairs and remembers the original insertion order of the keys.

const
    customSort = (a, b) => a - b,                           // take your sort function
    sortBy = fn => (a, b) => fn(a[0], b[0]);

var map = new Map([['a', 2], ['c', 1], ['d', 5]]),
    sorted = new Map([...map].sort(sortBy(customSort)));

console.log([...map]);
console.log([...sorted]);
.as-console-wrapper { max-height: 100% !important; top: 0; }
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392