admin管理员组文章数量:1323335
Considering that I have an object like the following where it's possible to have many names and that 'The others' can appear at any index, how can I sort the array having 'The others' always as the first element and the rest of the names sorted in alphabetical order?
var friends = [
{ id: 1, name: 'Paul' },
{ id: 2, name: 'Mary' },
{ id: 3, name: 'The others' },
{ id: 4, name: 'John' }
];
For the sample array above, the desired result would be:
[
{ id: 3, name: 'The others' },
{ id: 4, name: 'John' },
{ id: 2, name: 'Mary' },
{ id: 1, name: 'Paul' }
]
Considering that I have an object like the following where it's possible to have many names and that 'The others' can appear at any index, how can I sort the array having 'The others' always as the first element and the rest of the names sorted in alphabetical order?
var friends = [
{ id: 1, name: 'Paul' },
{ id: 2, name: 'Mary' },
{ id: 3, name: 'The others' },
{ id: 4, name: 'John' }
];
For the sample array above, the desired result would be:
[
{ id: 3, name: 'The others' },
{ id: 4, name: 'John' },
{ id: 2, name: 'Mary' },
{ id: 1, name: 'Paul' }
]
Share
Improve this question
edited Dec 6, 2017 at 4:04
Vinicius Santana
asked Dec 6, 2017 at 3:58
Vinicius SantanaVinicius Santana
4,1063 gold badges27 silver badges44 bronze badges
5
-
1
check for
the others
as a value in the sort callback function, return appropriate value (-1 or 1) regardless of lexical order of the two elements being pared – Jaromanda X Commented Dec 6, 2017 at 4:00 - I've changed the resulting array. Sorry for the confusion, but I need an array of sorted objects. – Vinicius Santana Commented Dec 6, 2017 at 4:04
- of course you do - the ment remains – Jaromanda X Commented Dec 6, 2017 at 4:04
- 2 So did you write a sort function that does the other half? – epascarello Commented Dec 6, 2017 at 4:05
- It's a little hard to pick the best answer for this one. Really good stuff on the answers. – Vinicius Santana Commented Dec 6, 2017 at 4:43
7 Answers
Reset to default 5Simply check for either value in the sort callback being The others
- return -1
if a
is that, or 1
if it's b
- otherwise return the localeCompare of a and b
friends.sort(({name: a}, {name:b}) => a == "The others" ? -1 : (b == "The others" ? 1 : a.localeCompare(b)));
The "readable" and non-ES2015+ version of that
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);
});
Sort like this:
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 es first
} else if (b.name == "The others") {
return 1;
} else {
return (a.name < b.name ? -1 : 1);
}
});
console.log(friends);
Following are steps that you need to follow
- Sort : https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort
- Move : Move an array element from one array position to another
Please see following working example.
var friends = [{
id: 1,
name: 'Paul'
},
{
id: 2,
name: 'Mary'
},
{
id: 3,
name: 'The others'
},
{
id: 4,
name: 'John'
}
];
// https://developer.mozilla/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./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);
- copy your (
The others
object) and store it into a variable usingfilter
- remove it from the array using
filter
- sort the array by name property.
- add the (
The others
object) in the beginning of your array usingunshift
pare
function copied from this answer
var friends = [{
id: 1,
name: 'Paul'
},
{
id: 2,
name: 'Mary'
},
{
id: 3,
name: 'The others'
},
{
id: 4,
name: 'John'
}
];
function pare(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(pare) // storing the array by name
// Adding the others object in the first of the array
newFriends.unshift(theOthersObj);
console.log(newFriends);
You want to hard code your pare function so that 'The others' always has the lowest/greatest value.
var friends = [
{ id: 1, name: 'Paul' },
{ id: 2, name: 'Mary' },
{ id: 3, name: 'The others' },
{ id: 4, name: 'John' }
];
function pare(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(pare))
Try the following:
var friends = [
{ id: 1, name: 'Paul' },
{ id: 2, name: 'Mary' },
{ id: 3, name: 'The others' },
{ id: 4, name: 'John' }
];
var notConsidered = friends.filter(friend => friend.name == 'The others');
var friend = friends.filter(friend => friend.name!= 'The others');
var sortedfriend = friend.sort(function (a, b) {
if (a.name < b.name) return -1;
else if (a.name > b.name) return 1;
return 0;
});
sortedfriend.unshift(notConsidered[0])
console.log(sortedfriend);
There are some great ideas here but what I found to be the simpler solutions are the two bellow:
Using sort:
friends.sort((a, b) => a.name !== b.name ? a.name < b.name ? -1 : 1 : 0).sort((a, b) => +(!b.name.localeCompare("The others")));
Using map and sort:
friends.map(o => o.name).sort((a,b) => a=="The others" ? -1 : b=="The others" ? 1 : a > b);
本文标签: javascriptSort an array of names except for one itemStack Overflow
版权声明:本文标题:javascript - Sort an array of names except for one item - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742135946a2422369.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论