4

I have many lists that need to be sorted according to specified interest. Alphabetical and numerical sorts using the array of values will not work.

For an example. The below cities, need to appear in this order. How can I accomplish this using JavaScript? Is there a way, I can represent each city with a numerical value then use the numerical values to sort the cities to the desired order below?

Salisbury, High Point, Wake, Charlotte, Raleigh, Cary, Wilson

1
  • You can place cities in object, and add order property for every city, for example... Commented Sep 18, 2015 at 19:56

4 Answers 4

11

Put the keys into a map:

var cityScores = {
  Salisbury: 5, High Point: 10, Wake: 15, Charlotte: 20, Raleigh: 25, Cary: 30, Wilson: 35
};

Then when you sort you can just refer to the scorings in that object:

cityList.sort(function(city1, city2) {
  return cityScores[city1] - cityScores[city2];
});

If the array is an array of objects with the city in it, then:

objectList.sort(function(o1, o2) {
  return cityScores[o1.city] - cityScores[o2.city];
});
Sign up to request clarification or add additional context in comments.

Comments

3

A solution for more than only the listed words, with precedence of the listed words.

var data = [
        'Salisbury', 'Charlotte', 'Frankfurt', 'Düsseldorf', 'Raleigh',
        'Cary', 'Wilson', 'High Point', 'Wake', 'New Jersey'
    ],
    customSort = [
        'Salisbury', 'High Point', 'Wake', 'Charlotte', 'Raleigh',
        'Cary', 'Wilson'
    ],
    customLookup = customSort.reduce(function (r, a, i) {
        r[a] = ('000'+i).slice(-4); return r;
    }, {}),
    customSortFn = function (a, b) {
        return (customLookup[a] || a).localeCompare(customLookup[b] || b);
    };

document.write('<pre>' + JSON.stringify(data.sort(customSortFn), 0, 4) + '</pre>');

2 Comments

How would this code be adjusted to work for doing custom sorting in a pivot table?
do you have an example, what you mean?
1

Array.prototype.sort() can use a compareFunction to sort elements:

var arr = []; 

arr.sort(compare);

/** copy paste from mdn - create whatever logic to sort by **/
function compare(a, b) {
  if (a is less than b by some ordering criterion) {
    return -1;
  }
  if (a is greater than b by the ordering criterion) {
    return 1;
  }
  // a must be equal to b
  return 0;
}

Comments

0

A simplified version of Nina Scholz's solution, putting the unrecognized elements at the end, without changing their order relative to each other. To avoid dealing with localeCompare.

var data = [
    'Salisbury', 'Charlotte', 'Frankfurt', 'Düsseldorf', 'Raleigh',
    'Cary', 'Wilson', 'High Point', 'Wake', 'New Jersey'
];
var priority = {
    'Salisbury': 10, 'High Point': 20, 'Wake': 30, 'Charlotte': 40,
    'Raleigh': 50, 'Cary': 60, 'Wilson': 70, // 'OTHER': 9999
};
data.sort( function (a, b) {
    return (priority[a] || 9999) - (priority[b] || 9999);
});

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.