Javascript Arbitrary sort array based on values of a field

Jeka

So I have an array of object which looks like this:

var myArray = [{priority : "low"}, {priority: "critical"}, {priority: "high"}]

I need to sort in this way: 1)Critical, 2) High and 3) Low.

how can this be done?

Nina Scholz

I suggest to use an object for the storing of the sort order.

If you need a default value for sorting, you could use a value for sorting unknown priority to start or to the end.

var sort = ['critical', 'high', 'low'],
    defaultValue = Infinity,
    sortObj = {},
    myArray = [{ priority: "unknown" }, { priority: "low" }, { priority: "critical" }, { priority: "high" }];

sort.forEach(function (a, i) { sortObj[a] = i + 1; });

myArray.sort(function (a, b) {
    return (sortObj[a.priority] || defaultValue) - (sortObj[b.priority] || defaultValue);
});
	
console.log(myArray);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Javascript Arbitrary sort array based on values of a field

From Dev

Sort array of objects by arbitrary list in JavaScript

From Dev

Sort array of objects by arbitrary list in JavaScript

From Java

What is the shortest way to simply sort an array of structs by (arbitrary) field names?

From Dev

Sort Array based on multiple values

From Dev

Sort a Django queryset based on the values in the choice field

From Dev

Sort one array based on values of another array?

From Dev

Sort an JSON array based on array values

From Dev

How to sort documents based on length of an Array field

From Dev

JavaScript sort array by 2 values

From Dev

Sort array with multiple values in Javascript

From Dev

Php sort array on based of another array and one sub array field

From Dev

Sort an array of objects based on values in the objects

From Dev

Sort Dictionary by Key Based on Values in Array

From Dev

Sort array by values of other array in Javascript?

From Dev

Adding an array field to a collection based on values of another array field in mongodb

From Dev

Javascript array sorting based on values

From Dev

How to Sort Array based on Object Value in JavaScript?

From Dev

Sort a Javascript array based on every other element

From Dev

sort 2 array with the values of one of them in javascript

From Dev

Create a function to sort an array of objects by their values in Javascript

From Dev

Javascript sort array of multiple values in one index

From Dev

Sorting Array based on an arbitrary index

From Dev

Sort numpy array by row and order matching values based on original array

From Dev

How to sort one array based on the values of other array in Java?

From Dev

Sort one array based on another array using javascript

From Dev

Javascript Sort array order based on another array order using underscore

From Dev

JQuery Sort Array of DOM Objects based off DOM values

From Dev

How to sort a numpy array based on the values in a specific row?

Related Related

  1. 1

    Javascript Arbitrary sort array based on values of a field

  2. 2

    Sort array of objects by arbitrary list in JavaScript

  3. 3

    Sort array of objects by arbitrary list in JavaScript

  4. 4

    What is the shortest way to simply sort an array of structs by (arbitrary) field names?

  5. 5

    Sort Array based on multiple values

  6. 6

    Sort a Django queryset based on the values in the choice field

  7. 7

    Sort one array based on values of another array?

  8. 8

    Sort an JSON array based on array values

  9. 9

    How to sort documents based on length of an Array field

  10. 10

    JavaScript sort array by 2 values

  11. 11

    Sort array with multiple values in Javascript

  12. 12

    Php sort array on based of another array and one sub array field

  13. 13

    Sort an array of objects based on values in the objects

  14. 14

    Sort Dictionary by Key Based on Values in Array

  15. 15

    Sort array by values of other array in Javascript?

  16. 16

    Adding an array field to a collection based on values of another array field in mongodb

  17. 17

    Javascript array sorting based on values

  18. 18

    How to Sort Array based on Object Value in JavaScript?

  19. 19

    Sort a Javascript array based on every other element

  20. 20

    sort 2 array with the values of one of them in javascript

  21. 21

    Create a function to sort an array of objects by their values in Javascript

  22. 22

    Javascript sort array of multiple values in one index

  23. 23

    Sorting Array based on an arbitrary index

  24. 24

    Sort numpy array by row and order matching values based on original array

  25. 25

    How to sort one array based on the values of other array in Java?

  26. 26

    Sort one array based on another array using javascript

  27. 27

    Javascript Sort array order based on another array order using underscore

  28. 28

    JQuery Sort Array of DOM Objects based off DOM values

  29. 29

    How to sort a numpy array based on the values in a specific row?

HotTag

Archive