排序除一个项目之外的名称数组

时间:2017-12-06 03:58:46

标签: javascript arrays sorting

考虑到我有一个像下面这样的对象,它可能有很多名字,而其他人可能会有很多名字。可以出现在任何索引处,我如何对具有'其他'的数组进行排序?始终作为第一个元素,其余的名称按字母顺序排序?

var friends = [
{ id: 1, name: 'Paul' },
{ id: 2, name: 'Mary' },
{ id: 3, name: 'The others' },
{ id: 4, name: 'John' }
];

对于上面的示例数组,所需的结果为:

[
   { id: 3, name: 'The others' }, 
   { id: 4, name: 'John' }, 
   { id: 2, name: 'Mary' }, 
   { id: 1, name: 'Paul' }
]

7 个答案:

答案 0 :(得分:2)

只需检查排序回调中的任何一个值为The others - 如果-1a则返回1,如果是b则返回friends.sort(({name: a}, {name:b}) => a == "The others" ? -1 : (b == "The others" ? 1 : a.localeCompare(b))); - 否则返回a和b的localeCompare

friends.sort(function (a, b) {
  if (a.name == "The others") return -1;
  if (b.name == "The others") return 1;
  return a.name.localeCompare(b.name);
});

的“可读”和非ES2015 +版本
var friends = [{
    id: 1,
    name: 'Paul'
  },
  {
    id: 2,
    name: 'Mary'
  },
  {
    id: 3,
    name: 'The others'
  },
  {
    id: 4,
    name: 'John'
  }
];

// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
friends.sort(function(a, b) {
  var nameA = a.name.toUpperCase(); // ignore upper and lowercase
  var nameB = b.name.toUpperCase(); // ignore upper and lowercase
  if (nameA < nameB) {
    return -1;
  }
  if (nameA > nameB) {
    return 1;
  }

  // names must be equal
  return 0;
});

var indexOfTheOters = friends.reduce(function(acc, friend, index) {
  if(!acc && friend.name === 'The others') {
    acc = index;
  }
  return acc;
}, null);

// https://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another
Array.prototype.move = function (old_index, new_index) {
    if (new_index >= this.length) {
        var k = new_index - this.length;
        while ((k--) + 1) {
            this.push(undefined);
        }
    }
    this.splice(new_index, 0, this.splice(old_index, 1)[0]);
    return this; // for testing purposes
};
friends.move(indexOfTheOters, 0)
console.log(friends);

答案 1 :(得分:1)

以下是您需要遵循的步骤

  1. 排序:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
  2. 移动:Move an array element from one array position to another
  3. 请参阅以下工作示例。

    original_list.group_by {|x| x[:number]}.values
    

答案 2 :(得分:1)

尝试以下方法:

&#13;
&#13;
The others
&#13;
&#13;
&#13;

答案 3 :(得分:1)

  1. 复制您的(filter对象)并使用filter
  2. 将其存储到变量中
  3. 使用The others
  4. 将其从阵列中删除
  5. 按名称属性对数组进行排序。
  6. 使用unshift
  7. 在数组的开头添加(compare对象) this answer

    复制的

    var friends = [{ id: 1, name: 'Paul' }, { id: 2, name: 'Mary' }, { id: 3, name: 'The others' }, { id: 4, name: 'John' } ]; function compare(a,b) { if (a.name < b.name) return -1; if (a.name > b.name) return 1; return 0; } // copy the others object const theOthersObj = friends.filter(friend => friend.name === 'The others')[0]; const newFriends = friends .filter(friend => friend.name !== 'The others') // removing the others object .sort(compare) // storing the array by name // Adding the others object in the first of the array newFriends.unshift(theOthersObj); console.log(newFriends);函数

    &#13;
    &#13;
    SELECT c.customer# "C#", o.order# "ORDER#",
           o.orderdate "ORDER DATE",
           NVL2(o.shipdate, 'Not shipped', 'shipped'),
           o.shipdate "Ship date",
           o.shipdate-o.orderdate AS "lead time in days",
    TRUNC((o.shipdate-o.orderdate)/30), 2) AS "lead time in months"
    FROM customers c, orders o
    ORDER BY "lead time in days" DESC, c.customer# ASC, o.order# ASC;
    
    &#13;
    &#13;
    &#13;

答案 4 :(得分:1)

按这样排序:

&#13;
&#13;
var friends = [
   { id: 1, name: 'Paul' },
   { id: 2, name: 'Mary' },
   { id: 3, name: 'The others' },
   { id: 4, name: 'John' }
];

friends.sort((a, b) => {
     if (a.name == "The others") {
         return -1; // a comes first
     } else if (b.name == "The others") {
         return 1;
     } else {
         return (a.name < b.name ? -1 : 1);
     }
 });

console.log(friends);
&#13;
&#13;
&#13;

答案 5 :(得分:1)

您希望对比较功能进行硬编码,以便“其他人”能够对其进行硬编码。始终具有最低/最高价值。

&#13;
&#13;
var friends = [
{ id: 1, name: 'Paul' },
{ id: 2, name: 'Mary' },
{ id: 3, name: 'The others' },
{ id: 4, name: 'John' }
];

function compare(a, b){
  if(a.name === "The others") {
  	return -1;
  } else if (b.name === "The others") {
  	return 1;
  } else if (a.name > b.name){
  	return -1;
  } else if (a.name < b.name){
  	return 1;
  } else {
  	return 0;
  }
}

console.log(friends.sort(compare))
&#13;
&#13;
&#13;

答案 6 :(得分:0)

这里有一些很棒的想法,但我发现更简单的解决方案是下面两个:

使用sort:

friends.sort((a, b) => a.name !== b.name ? a.name < b.name ? -1 : 1 : 0).sort((a, b) => +(!b.name.localeCompare("The others")));

使用地图和排序:

friends.map(o => o.name).sort((a,b) => a=="The others" ? -1 : b=="The others" ? 1 : a > b);