js 數組sort, 多條件排序。


Array.sort(); 

sort()方法可以傳入一個函數作為參數,然后依據該函數的邏輯,進行數組的排序。

一般用法:(數組元素從小大進行排序)

 

var a = [9, 6, 5, 7, 11, 52, 15];
a.sort(function(return a-b;));

   sort()方法,接收了一個函數作為參數時,排序主要根據傳入函數的返回值是否大於0進行排序。

   1)當 a - b < 0  時, 則 a 元素排在 b 元素的前面。

   2) 當 a - b = 0 時, a , b 元素的位置不變。

   3) 當 a - b > 0 是, 則 b 元素排在 a 元素的前面。

 

----------------------------------------------------------------------------------------------------------------------

以上是針對單純的整數數組,當數組的元素為對象時,則可能需要根據對象的屬性進行排序,此時就會涉及到多條件排序。

var obj = {online:  true,  id: 123,  mes: 3}; //online表示是否在線, id:玩家ID,mes:消息條數;

如數組中的元素是obj對象,此時需要根據, online, id, mes 三個維度對數組進行排序。

需求如下:online為true排前面, id 小的排前面,mes>0的排在前面

直接上代碼:

 1 function Obj(id, online, mes){
 2     this.id = id;
 3     this.online = online;
 4     this.mes = mes
 5 }
 6 
 7 function mysort(a ,b){
 8     //online 都為true
 9     if(a.online && b.online){
10         //mes都 大於 0
11         if(a.mes && b.mes){
12             return a.id < b.id ? -1 : 1;
13         }
14         //mes都 等於 0
15         else if(!a.mes && !b.mes){
16             return a.id < b.id ? -1 : 1;
17         }
18         //mes 不同時為 0 
19         else{
20             return a.mes > b.mes ? -1 : 1;
21         }
22     }
23     //online 都為false
24     else if(!a.online && !b.online){
25         if(a.mes && b.mes){
26             return a.id < b.id ? -1 : 1;
27         }
28         else if(!a.mes && !b.mes){
29             return a.id < b.id ? -1 : 1;
30         }
31         else{
32             return a.mes > b.mes ? -1 : 1;
33         }
34     }
35     //online 不同時為true
36     else{
37         return a.online > b.online ? -1 : 1;
38     }
39 }
40 
41 var arr = [];
42 for(var i = 0; i < 10; i++){
43     var id = parseInt(Math.random() * 10);
44     var mes = parseInt(Math.random() * 10);
45     if(id > 5){
46         var online = true;
47     }
48     else var online = false;
49     arr[i] = new Obj(id, online, mes);
50 }
51 console.table(arr);
52 arr.sort(mysort);
53 console.table(arr);

初始化的數組:

 

排序后的結果:

 

所以遇到多條件排序的需求時,先一級一級進行划分,最后再根據某個條件進行排序。

function Obj( id, online, mes){
this. id = id;
this. online = online;
this. mes = mes
}

function mysort( a , b){
//online 都為true
if( a. online && b. online){
//mes都 大於 0
if( a. mes && b. mes){
return a. id < b. id ? - 1 : 1;
}
//mes都 等於 0
else if(! a. mes && ! b. mes){
return a. id < b. id ? - 1 : 1;
}
//mes 不同時為 0
else{
return a. mes > b. mes ? - 1 : 1;
}
}
//online 都為false
else if(! a. online && ! b. online){
if( a. mes && b. mes){
return a. id < b. id ? - 1 : 1;
}
else if(! a. mes && ! b. mes){
return a. id < b. id ? - 1 : 1;
}
else{
return a. mes > b. mes ? - 1 : 1;
}
}
//online 不同時為true
else{
return a. online > b. online ? - 1 : 1;
}
}

var arr = [];
for( var i = 0; i < 10; i++){
var id = parseInt( Math. random() * 10);
var mes = parseInt( Math. random() * 10);
if( id > 5){
var online = true;
}
else var online = false;
arr[ i] = new Obj( id, online, mes);
}
console. table( arr);
arr. sort( mysort);
console. table( arr);


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM