0

This is my array:

let a = [
    {
        IsGroup: true,
        Name: "Antonia Doyle"
    },
    {
        IsGroup: false,
        Name: "Dana Gray"
    },
    {
        IsGroup: false,
        Name: "Amber Banks"
    },
    {
        IsGroup: false,
        Name: "Geoff Neal"
    },
    {
        IsGroup: true,
        Name: "Nina Hartley"
    },
    {
        IsGroup: false,
        Name: "Elizabeth Warren"
    },
    {
        IsGroup: false,
        Name: "Ghengis Khan"
    },
    {
        IsGroup: true,
        Name: "Masta Razz"
    }
];

I can't see to sort it by IsGroup and then Name. However, it doesn't seem to be working. My code so far:

let b = [];
b = a;

b.sort(byGroupThenName);

function byGroupThenName(a, b) {
    return b.IsGroup - a.IsGroup || (a.Name - b.Name ? -1 : 1);
}
3
  • 1
    So you want to group your array by isGroup field and then sort the groups. is my understanding right? Commented Nov 24, 2020 at 13:44
  • FYI Subtracting two non-numeric strings (!) always produces NaN, regardless of the values of the non-numeric strings. Commented Nov 24, 2020 at 13:45
  • 1
    Subtracting strings does not make sense. It should be a.Name.localeCompare(b.Name), which will give the exact kind of result you need for sorting. Commented Nov 24, 2020 at 13:45

2 Answers 2

2

Use the dynamicSort() function which takes two params property and order (asc & desc)

let a = [ { IsGroup: true , Name: "Antonia Doyle" },
      { IsGroup: false, Name: "Dana Gray" },
      { IsGroup: false, Name: "Amber Banks" },
      { IsGroup: false, Name: "Geoff Neal" },
      { IsGroup: true , Name: "Nina Hartley" },
      { IsGroup: false, Name: "Elizabeth Warren" },
      { IsGroup: false, Name: "Ghengis Khan" },
      { IsGroup: true , Name: "Masta Razz" } ]

const dynamicSort = (property, order) => {
  let sortOrder = 1
  if (order === 'desc') {
    sortOrder = -1
  }
  return function(a, b) {
    // a should come before b in the sorted order
    if (a[property] < b[property]) {
      return -1 * sortOrder
      // a should come after b in the sorted order
    } else if (a[property] > b[property]) {
      return 1 * sortOrder
      // a and b are the same
    } else {
      return 0 * sortOrder
    }
  }
}

const sortedArr = a.sort(dynamicSort('IsGroup','desc'))

console.log(sortedArr)

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

5 Comments

But we cannot pass another property(ie Name)?
you can pass any property to it
Sorry let me rephrase, can I pass more then one property to it?
No, just one prop
In this case, I'm going to mark Slai's answer as the correct answer(it is my main requirement). However, +1 for a great suggestion. Thank you.
1

localeCompare can be used to compare strings :

let arr = [ { IsGroup: true , Name: "Antonia Doyle" },
          { IsGroup: false, Name: "Dana Gray" },
          { IsGroup: false, Name: "Amber Banks" },
          { IsGroup: false, Name: "Geoff Neal" },
          { IsGroup: true , Name: "Nina Hartley" },
          { IsGroup: false, Name: "Elizabeth Warren" },
          { IsGroup: false, Name: "Ghengis Khan" },
          { IsGroup: true , Name: "Masta Razz" } ]
    
arr.sort((a, b) => b.IsGroup - a.IsGroup || a.Name.localeCompare(b.Name))

console.log( JSON.stringify(arr).replace(/},/g, '},\n ') )

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.