最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

sorting - How to custom sort in JavaScript? Not alphabetical or numerical - Stack Overflow

programmeradmin4浏览0评论

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 acplish 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

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 acplish 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

Share Improve this question asked Sep 18, 2015 at 19:51 user2267035user2267035 731 silver badge5 bronze badges 1
  • You can place cities in object, and add order property for every city, for example... – sinisake Commented Sep 18, 2015 at 19:56
Add a ment  | 

4 Answers 4

Reset to default 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];
});

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>');

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

var arr = []; 

arr.sort(pare);

/** copy paste from mdn - create whatever logic to sort by **/
function pare(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;
}

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);
});
发布评论

评论列表(0)

  1. 暂无评论