alex
alex

Reputation: 7601

Modifying the following sort function so it returns numbers

The following function does the following:

1) If isDefault === true and a isDefault === false, the former goes first.

2) If isDefault === true and a isDefault === true, sort by updatedAt in a descending fashion.

3) If isDefault === false and a isDefault === false, sort by name in an ascending fashion.

function sortPanoramas (panoramas) {
  panoramas.sort((a, b) => {
    if (a.isDefault !== b.isDefault) return a.isDefault < b.isDefault
    if (a.isDefault && b.isDefault) return a.updatedAt < b.updatedAt
    if (!a.isDefault && !b.isDefault) return a.name > b.name
  })
}

This works in Chrome but not in Safari, because I was told that you're supposed to return numbers in sort functions.

How to modify the function so it return numbers?

EDIT: Sample data:

[
  {
    "id": "CbU2Z5BZ9w",
    "name": "a01",
    "updatedAt": "2016-08-24T06:20:47.972Z",
    "isDefault": true,
    "index": 0
  },
  {
    "id": "RdT5CvGLRg",
    "name": "a02",
    "updatedAt": "2016-08-24T06:21:22.126Z",
    "isDefault": false,
    "index": 1
  },
  {
    "id": "eHyt1TYcBN",
    "name": "a03",
    "updatedAt": "2016-08-24T06:21:47.404Z",
    "isDefault": false,
    "index": 2
  },
  {
    "id": "jUTmyn2TDR",
    "name": "a04",
    "updatedAt": "2016-08-24T06:26:29.591Z",
    "isDefault": false,
    "index": 3
  },
  {
    "id": "VGXXNcBMcH",
    "name": "a05",
    "updatedAt": "2016-08-24T06:27:12.934Z",
    "isDefault": false,
    "index": 4
  }
]

Upvotes: 1

Views: 62

Answers (4)

Xotic750
Xotic750

Reputation: 23482

In your logic, you state

1) If isDefault === true and a isDefault === false, the former goes first.

2) If isDefault === true and a isDefault === true, sort by updatedAt in a descending fashion.

3) If isDefault === false and a isDefault === false, sort by name in an ascending fashion.

But what do you expect to happen if none of these match, ie when a.isDefault === false && b.isDefault === true? No sorting should occur?

If so then this could be a solution.

function sortPanoramas(panoramas) {
  panoramas.sort((a, b) => {
    if (a.isDefault) {
      return b.isDefault ? a.updatedAt.localeCompare(b.updateAt) : -1;
    }
    return b.isDefault ? 0 : a.name.localeCompare(b.name);
  })
}

Perhaps you could clarify the logic?

Upvotes: 0

user94559
user94559

Reputation: 60143

I think this is what you need:

function sortPanoramas(panoramas) {
  panoramas.sort((a, b) => {
    if (a.isDefault !== b.isDefault) {
      return a.isDefault ? -1 : 1;
    }
    if (a.isDefault && b.isDefault) {
      return a.updatedAt > b.updatedAt ? -1 : a.updatedAt < b.updatedAt ? 1 : 0;
    }
    return a.name < b.name ? -1 : a.name > b.name ? 1 : 0;
  });
}

Upvotes: 2

Nina Scholz
Nina Scholz

Reputation: 386654

You could just chain the conditions

function sortPanoramas(panoramas) {
    panoramas.sort((a, b) => !a.isDefault - !b.isDefault ||
                             a.isDefault && b.updatedAt.localeCompare(a.updatedAt) ||
                             a.name.localeCompare(b.name));
}

var data = [{ "id": "eHyt1TYcBN", "name": "a03", "updatedAt": "2016-08-24T06:21:47.404Z", "isDefault": false, "index": 2 }, { "id": "VGXXNcBMcH", "name": "a05", "updatedAt": "2016-08-24T06:27:12.934Z", "isDefault": false, "index": 4 }, { "id": "jUTmyn2TDR", "name": "a04", "updatedAt": "2016-08-24T06:26:29.591Z", "isDefault": false, "index": 3 }, { "id": "CbU2Z5BZ9w", "name": "a01", "updatedAt": "2016-08-24T06:20:47.972Z", "isDefault": true, "index": 0 }, { "id": "RdT5CvGLRg", "name": "a02", "updatedAt": "2016-08-24T06:21:22.126Z", "isDefault": false, "index": 1 }, ];

sortPanoramas(data);
console.log(data);

Upvotes: 2

DhruvPathak
DhruvPathak

Reputation: 43245

You need to return positive or negative numbers based on your boolean conditions.

function sortPanoramas (panoramas) {
  var mapping = { true:1, false : -1} //example mapping
  panoramas.sort((a, b) => {
    if (a.isDefault !== b.isDefault) return mapping[a.isDefault < b.isDefault]

  })
}

Upvotes: 1

Related Questions