JavaScript的排序功能。排序第一,而第二位 [英] Javascript sort function. Sort by First then by Second
本文介绍了JavaScript的排序功能。排序第一,而第二位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有对象sort.Each对象的数组有两个参数:强度和名称
I have an array of objects to sort.Each object has two parameters: Strength and Name
objects = []
object[0] = {strength: 3, name: "Leo"}
object[1] = {strength: 3, name: "Mike"}
我想首先强度,然后按字母顺序进行排序。我使用下面的code。通过第一个参数排序。我如何排序,然后由第二?
I want to sort first by Strength and then alphabetically. I am using the following code to sort by the first parameter. How do I sort then by the second?
function sortF(ob1,ob2) {
if (ob1.strength > ob2.strength) {return 1}
else if (ob1.strength < ob2.strength){return -1}
return 0;
};
感谢您的帮助。
Thanks for your help.
推荐答案
展开您的排序功能是这样的;
Expand your sort function to be like this;
function sortF(ob1,ob2) {
if (ob1.strength > ob2.strength) {
return 1;
} else if (obj1.strength < obj2.strength) {
return -1;
}
// Else go to the 2nd item
if (ob1.name < ob2.name) {
return -1;
} else if (ob1.name > ob2.name) {
return 1
} else { // nothing to split them
return 0;
}
}
A &LT;
和&gt;在字符串的
比较字母比较。
A <
and >
comparison on strings is an alphabetic comparison.
这篇关于JavaScript的排序功能。排序第一,而第二位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文