javaScript 關於一系列對象的排序,最終還是把對象壓入到數組中利用數組的sort排序比較方便。

//-- 給一組對象排序
//-- 例如有這樣的一組值: {name:"abc",value:10},{name:"dbc",value:5},{name:"acc",value:15},{name:"ebc",value:50},{name:"bbc",value:80},{name:"aca",value:60}
//-- 將這組值壓入數組。利用數組進行排序。
//-- 利用自定義的函數比較數值
let obj = [{name:"abc",value:10},{name:"dbc",value:5},{name:"acc",value:15},{name:"ebc",value:50},{name:"bbc",value:80},{name:"aca",value:60}];
function compare(a, b){
    //根據數組中某元素中的,某數值進行排序。
    //前減後,是升序
    return a.value - b.value; 
}
function compare1(a,b){
    //根據數組中某元素中的,某字符串進行排序。
    //localeCompare 字符串比較
	return a["name"].localeCompare(b["name"]);											
}
obj.sort(compare);
console.log(obj); console.log("\n");
obj.sort(compare1);
console.log(obj); console.log("\n");
// // 無參sort方法會調用每個數組項的toString()方法,得到字符串,然後再對得到的字符串進行排序,有時候可能得不到我們想要的結果
let obj = [{x: 10},3,0,-1,11,99,0,1,55];
obj.sort();
console.log(obj); //[ -1, 0, 0, 1, 11, 3, 55, 99, { x: 10 } ]
for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
        console.log( obj[key].toString());
    }
}
// // 無參sort方法會調用每個數組項的toString()方法,得到字符串,然後再對得到的字符串進行排序,有時候可能得不到我們想要的結果
let obj = ["10","3","0","-1","11","99","0","1","55"];
obj.sort(compare);
console.log(obj); //[ '-1', '0', '0', '1', '10', '11', '3', '55', '99' ]
function compare(a, b){
    return a.localeCompare(b);
}
// localeCompare 是字符對象的方法,ab都是字符串。
//-- 如果想要對對象的成員進行排序是無意義的。如果你想對對象排序後再使用對象,還不如使用數組。
例子1//能夠完成完成排序, 但對象轉成數組排序後,再由數組轉成對象不能保存key和value的對應關係(即轉換後和原對象不一樣)。
let obj = {"s":0,N:3,K:0,4:-1,5:11,6:99,7:0,8:1,9:55};
let arr = [];
for (const key in obj) {
    //-- 判斷是自身屬性,非繼承過來的屬性。
    if (obj.hasOwnProperty(key)) {
        const element = obj[key];
        console.log("element =  ", element);
        arr.push(element);
    }
}
console.log("\n begin :arr =  ", arr);
//-- 已經正確的進行了排序
arr.sort(function(a,b){return a - b;}) 
console.log("\n end :arr =  ", arr);
//-- end :arr =   [ -1, 0, 0, 0, 1, 3, 11, 55, 99 ]

obj = {};
for (const key in arr) {
    obj[key] = arr[key];
}
console.log("\n end :obj =  ", obj);
//--end :obj =   { '0': -1,
  '1': 0,
  '2': 0,
  '3': 0,
  '4': 1,
  '5': 3,
  '6': 11,
  '7': 55,
  '8': 99 }

例子2//-- 對象還原成數組排序,在還原成對象,此例子能夠完成key和value的對應關係,但依然不能完成排序。
//-- 如果想要對對象的成員進行排序是無意義的。如果你想對對象排序後再使用對象,還不如使用數組。
let obj = {"s":0,N:3,K:0,4:-1,5:11,6:99,7:0,8:1,9:55};
let arr = [];
for (const key in obj) {
    const element = obj[key];
    console.log("element =  ", element);
    arr[key] = element;
}
console.log("\n begin :arr =  ", arr); // begin :arr =   [ <4 empty items>, -1, 11, 99, 0, 1, 55, s: 0, N: 3, K: 0 ]
//-- 已經正確的進行了排序
arr.sort(function(a,b){return a - b;}) 
console.log("\n end :arr =  ", arr); // end :arr =   [ -1, 0, 1, 11, 55, 99, <4 empty items>, s: 0, N: 3, K: 0 ]
obj = {};
for (const key in arr) {
    
    obj[key] = arr[key];
    console.log("arr[key]==", arr[key]);
}
console.log("\n end :obj =  ", obj);
// end :obj =   { '0': -1,
// '1': 0,
// '2': 1,
// '3': 11,
// '4': 55,
// '5': 99,
// s: 0,
// N: 3,
// K: 0 }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章