Javascript 对除一项以外的名称数组进行排序
考虑到我有一个像下面这样的对象,其中可能有许多名称,“其他”可以出现在任何索引中,我如何排序数组,让“其他”始终作为第一个元素,其余名称按字母顺序排序Javascript 对除一项以外的名称数组进行排序,javascript,arrays,sorting,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
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' }
]
只需检查排序回调中的值是否为
其他值
-如果a
为该值,则返回-1
;如果b
为该值,则返回a和b的本地比较
friends.sort(({name: a}, {name:b}) => a == "The others" ? -1 : (b == "The others" ? 1 : a.localeCompare(b)));
“可读”和非ES2015+版本
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);
});
以下是您需要遵循的步骤
var-friends=[{
id:1,
名字:“保罗”
},
{
id:2,
名字:“玛丽”
},
{
id:3,
姓名:“其他人”
},
{
id:4,
名字:“约翰”
}
];
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
friends.sort(函数(a,b){
var nameA=a.name.toUpperCase();//忽略大小写
var nameB=b.name.toUpperCase();//忽略大小写
if(nameA名称B){
返回1;
}
//名字必须相等
返回0;
});
var indexOfTheOters=friends.reduce(函数(acc、friends、index){
如果(!acc&&friend.name=='其他人'){
acc=指数;
}
返回acc;
},空);
// https://stackoverflow.com/questions/5306680/move-an-array-element-from-one-array-position-to-another
Array.prototype.move=函数(旧索引、新索引){
如果(新索引>=此.length){
var k=新的_指数-this.length;
而((k--)+1){
这个。推(未定义);
}
}
this.splice(新的_索引,0,this.splice(旧的_索引,1)[0]);
返回此;//用于测试目的
};
朋友。移动(indexOfTheOters,0)
console.log(朋友)代码>请尝试以下操作:
var-friends=[
{id:1,名字:'Paul'},
{id:2,名字:'Mary'},
{id:3,名称:'其他'},
{id:4,名字:'John'}
];
var notseared=friends.filter(friend=>friend.name=='theothers');
var friend=friends.filter(friend=>friend.name!=“其他人”);
var sortedfriend=friend.sort(函数(a,b){
如果(a.nameb.name)返回1;
返回0;
});
sortedfriend.unshift(未考虑[0])
控制台日志(sortedfriend)代码>
复制您的(其他对象
对象)并使用过滤器将其存储到变量中
使用过滤器将其从阵列中删除
按名称属性对数组排序
使用unshift
比较
从复制的函数
var-friends=[{
id:1,
名字:“保罗”
},
{
id:2,
名字:“玛丽”
},
{
id:3,
姓名:“其他人”
},
{
id:4,
名字:“约翰”
}
];
功能比较(a、b){
如果(a.nameb.name)
返回1;
返回0;
}
//复制其他对象
const theOthersObj=friends.filter(friends=>friends.name===“其他人”)[0];
const newFriends=朋友
.filter(friend=>friend.name!=='theothers')//删除others对象
.sort(compare)//按名称存储数组
//将其他对象添加到数组的第一个
《新朋友》杂志(theOthersObj);
console.log(newFriends)代码>这里有一些很棒的想法,但我发现更简单的解决方案是以下两个:
使用排序:
friends.sort((a, b) => a.name !== b.name ? a.name < b.name ? -1 : 1 : 0).sort((a, b) => +(!b.name.localeCompare("The others")));
有点像这样:
var-friends=[
{id:1,名字:'Paul'},
{id:2,名字:'Mary'},
{id:3,名称:'其他'},
{id:4,名字:'John'}
];
friends.sort((a,b)=>{
如果(a.name==“其他人”){
return-1;//首先是a
}否则如果(b.name==“其他人”){
返回1;
}否则{
返回(a.name console.log(朋友)代码>您希望硬编码比较函数,以便“其他”始终具有最低/最大值
var-friends=[
{id:1,名字:'Paul'},
{id:2,名字:'Mary'},
{id:3,名称:'其他'},
{id:4,名字:'John'}
];
功能比较(a、b){
如果(a.name==“其他人”){
返回-1;
}否则如果(b.name==“其他人”){
返回1;
}else if(a.name>b.name){
返回-1;
}else if(a.name console.log(friends.sort(compare))
检查其他
作为排序回调函数中的值,返回适当的值(-1或1),而不考虑要比较的两个元素的词法顺序。我已更改生成的数组。很抱歉造成混淆,但我需要一个排序对象数组。当然,您需要-注释保留那么您是否编写了一个排序函数来完成另一半?要为这一个选择最佳答案有点困难。答案真的很好。
friends.map(o => o.name).sort((a,b) => a=="The others" ? -1 : b=="The others" ? 1 : a > b);