0

I have a list of object like this:

 [
      {"groups":["aaa"]},
      {"groups":["bbb"]},
      {"groups":["ccc"]},
      {"groups":["ddd"]},
      {"groups":["eee"]},
      {"groups":["fff"]},
      {"groups":["ggg"]},
      {"groups":["hhh"]},
      {"groups":["iii"]},
      {"groups":["mmm", "mmm"]}
      {"groups":["lll", "lll"]}
];

i want put all object with more that one groups at the end of the list in alphabetical order and keep the others with one groups, at beginning of the list without change the original order.

This is my code

var list = [
  {"groups":["aaa"]},
  {"groups":["bbb"]},
  {"groups":["ccc"]},
  {"groups":["ddd"]},
  {"groups":["eee"]},
  {"groups":["fff"]},
  {"groups":["ggg"]},
  {"groups":["hhh"]},
  {"groups":["iii"]},
  {"groups":["mmm", "mmm"]},
  {"groups":["lll", "lll"]}
];

list.sort(function(a, b){
        var aIsGroup = (a.groups.length > 1);
        var bIsGroup = (b.groups.length > 1);

        if (aIsGroup && !bIsGroup) {
            return 1;
        } else if (!aIsGroup && bIsGroup) {
            return -1;
        } else if(aIsGroup && bIsGroup){
            return a.groups[0].toLowerCase().localeCompare(b.groups[0].toLowerCase());
        }
        return 0;
});

console.log(list);

You can see in the snippet the current output, but the expected output is:

[
  {
    "groups": [
      "aaa"
    ]
  },
  {
    "groups": [
      "bbb"
    ]
  },
  {
    "groups": [
      "ccc"
    ]
  },
  {
    "groups": [
      "ddd"
    ]
  },
  {
    "groups": [
      "eee"
    ]
  },
  {
    "groups": [
      "fff"
    ]
  },
  {
    "groups": [
      "ggg"
    ]
  },
  {
    "groups": [
      "hhh"
    ]
  },
  {
    "groups": [
      "iii"
    ]
  },
  {
    "groups": [
      "lll",
      "lll"
    ]
  },
  {
    "groups": [
      "mmm",
      "mmm"
    ]
  }
]
9

1 Answer 1

0

as @deceze said in comment

Javascript's sort doesn't promise a stable sorting. Any elements that are "equal" have an undefined order. See the duplicate for implementing a stable sort

but you can do this to keep them in the same order as their original order (a stable sort)

var list = [
  {"groups":["aaa"]},
  {"groups":["bbb"]},
  {"groups":["ccc"]},
  {"groups":["mmm", "mmm"]},
  {"groups":["ddd"]},
  {"groups":["fff"]},
  {"groups":["eee"]},
  {"groups":["ggg"]},
  {"groups":["hhh"]},
  {"groups":["iii"]},
  {"groups":["lll", "lll"]}
];

list.sort(function(a, b){
        var aIsGroup = (a.groups.length > 1);
        var bIsGroup = (b.groups.length > 1);

        if (aIsGroup && !bIsGroup) {
            return 1;
        } else if (!aIsGroup && bIsGroup) {
            return -1;
        } else if(aIsGroup && bIsGroup) {
            return a.groups[0].toLowerCase().localeCompare(b.groups[0].toLowerCase());
        }
        // keep in the same order
        return (list.indexOf(a) < list.indexOf(b)) ? -1 : 1;
});

console.log(list);

Sign up to request clarification or add additional context in comments.

5 Comments

I'm not sure the criterion is to sort them lexically, but rather they should retain their original order (i.e. stable sort).
@deceze you may be correct need to ask op
@deceze rereading the question you're correct
@deceze, thanks but i want the alphabetical order only if (a.groups.length > 1) && (b.groups.length > 1)
@ar099968 edited ;) fff and eee are mixed because I mixed them when declaring array to show no lexical on monoelement

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.