欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS對(duì)象數(shù)組排序方法測(cè)試代碼示例

 更新時(shí)間:2024年06月20日 10:36:25   作者:從小白小菜開始  
這篇文章主要給大家介紹了關(guān)于JS對(duì)象數(shù)組排序方法測(cè)試的相關(guān)資料,在  相信大家對(duì)數(shù)組排序都不陌生,在開發(fā)中我們通常會(huì)使用sort方法進(jìn)行數(shù)組的排序,需要的朋友可以參考下

一.Array.prototype.sort()

1.默認(rèn)排序 sort()

sort() 方法就地對(duì)數(shù)組的元素進(jìn)行排序,并返回對(duì)相同數(shù)組的引用。默認(rèn)排序是將元素轉(zhuǎn)換為字符串,然后按照它們的 UTF-16 碼元值升序排序。

由于它取決于具體實(shí)現(xiàn),因此無法保證排序的時(shí)間和空間復(fù)雜度。

如果想要不改變?cè)瓟?shù)組的排序方法,可以使用 toSorted()。

說明:兩個(gè)重點(diǎn)。1、會(huì)改變?cè)瓟?shù)組。2、默認(rèn)按將元素轉(zhuǎn)換為字符串排序。

const months = ['March', 'Jan', 'Feb', 'Dec'];
months.sort();
console.log(months);
// Expected output: Array ["Dec", "Feb", "Jan", "March"]

const array1 = [1, 30, 4, 21, 100000];
array1.sort();
console.log(array1);
// Expected output: Array [1, 100000, 21, 30, 4]

2.比較函數(shù) sort(compareFn)

定義排序順序的函數(shù)。返回值應(yīng)該是一個(gè)數(shù)字,其符號(hào)表示兩個(gè)元素的相對(duì)順序:如果 a 小于 b,返回值為負(fù)數(shù),如果 a 大于 b,返回值為正數(shù),如果兩個(gè)元素相等,返回值為 0。NaN 被視為 0

說明:自定義比較函數(shù)返回一個(gè)數(shù)值。一般為1,-1,0.

function compareFn(a, b) {
  if (根據(jù)排序標(biāo)準(zhǔn),a 小于 b) {
    return -1;
  }
  if (根據(jù)排序標(biāo)準(zhǔn),a 大于 b) {
    return 1;
  }
  // a 一定等于 b
  return 0;
}
const stringArray = ["Blue", "Humpback", "Beluga"];
const numberArray = [40, 1, 5, 200];
const numericStringArray = ["80", "9", "700"];
const mixedNumericArray = ["80", "9", "700", 40, 1, 5, 200];

function compareNumbers(a, b) {
  return a - b;
}

stringArray.sort(); // ['Beluga', 'Blue', 'Humpback']

numberArray.sort(compareNumbers); // [1, 5, 40, 200]

numericStringArray.sort(); // ['700', '80', '9']
numericStringArray.sort(compareNumbers); // ['9', '80', '700']

mixedNumericArray.sort(compareNumbers); // [1, 5, '9', 40, '80', 200, '700']

二.對(duì)象數(shù)組

let arr = [  
  { name: 'Zhang', age: 25, score: 85 },  
  { name: 'Li', age: 20, score: 90 },  
  { name: 'Wang', age: 22, score: 80 },  
  { name: 'Zhao', age: 22, score: 92 }  
];  
  
// 按 age 升序排序,如果 age 相同則按 score 降序排序  
arr.sort((a, b) => {  
  if (a.age !== b.age) {  
    return a.age - b.age; // 按 age 升序排序  
  } else {  
    return b.score - a.score; // 如果 age 相同,按 score 降序排序  
  }  
});  
  
console.log(arr);
[  
  { name: 'Li', age: 20, score: 90 },  
  { name: 'Wang', age: 22, score: 80 },  
  { name: 'Zhao', age: 22, score: 92 },  
  { name: 'Zhang', age: 25, score: 85 }  
]

三.通用方法

1.指定單一對(duì)象元素屬性

function sortObjectsByProperty(array, property) {
    return array.sort(function(a, b) {
        if (a[property] < b[property]) {
            return -1;
        } else if (a[property] > b[property]) {
            return 1;
        } else {
            return 0;
        }
    });
}
var objects = [
    { name: 'Apple', price: 15 },
    { name: 'Banana', price: 10 },
    { name: 'Cherry', price: 20 }
];

var sortedObjects = sortObjectsByProperty(objects, 'price');
console.log(sortedObjects);

輸出:

[
  { name: 'Banana', price: 10 },
  { name: 'Apple', price: 15 },
  { name: 'Cherry', price: 20 }
]

2.指定對(duì)象元素多屬性

function sortObjectsByProperties(array, ...properties) {  
    return array.sort((a, b) => {  
        for (const property of properties) {  
            if (a[property] < b[property]) {  
                return -1;  
            } else if (a[property] > b[property]) {  
                return 1;  
            }  
            // 如果屬性相等,則繼續(xù)比較下一個(gè)屬性  
        }  
        // 所有屬性都相等  
        return 0;  
    });  
}  
  
// 示例對(duì)象數(shù)組  
const employees = [  
    { name: 'Alice', age: 30, salary: 50000 },  
    { name: 'Bob', age: 25, salary: 60000 },  
    { name: 'Charlie', age: 35, salary: 55000 },  
];  
  
// 使用剩余參數(shù)傳入多個(gè)屬性進(jìn)行排序  
const sortedEmployees = sortObjectsByProperties(employees, 'age', 'salary');  
  
console.log(sortedEmployees);

輸出:

[  
    { name: 'Bob', age: 25, salary: 60000 }, // 年齡最小  
    { name: 'Alice', age: 30, salary: 50000 }, // 年齡次小,但薪水低于Charlie  
    { name: 'Charlie', age: 35, salary: 55000 } // 年齡最大,薪水也最高(在同齡人中)  
]

四. 對(duì)象數(shù)組漢字按拼音排序

1.使用stringObject.localeCompare(target)

const chinesePeople = [  
    { name: '張三', age: 30 },  
    { name: '李四', age: 25 },  
    { name: '王五', age: 35 },  
    { name: '趙六', age: 40 },  
];  
  
// 使用 localeCompare 對(duì)名字屬性進(jìn)行排序  
chinesePeople.sort((a, b) => a.name.localeCompare(b.name, 'zh-CN'));  
  
console.log(chinesePeople);

輸出:

[
  { name: '李四', age: 25 },
  { name: '王五', age: 35 },
  { name: '張三', age: 30 },
  { name: '趙六', age: 40 } 
]

2.使用第三方庫(kù)

如果你想要根據(jù)漢字拼音對(duì)對(duì)象數(shù)組進(jìn)行排序,你需要先將漢字轉(zhuǎn)換為拼音,然后根據(jù)拼音進(jìn)行排序。這通常需要使用到第三方庫(kù)來實(shí)現(xiàn)漢字到拼音的轉(zhuǎn)換,比如 pinyin 庫(kù)。

npm install pinyin
const pinyin = require('pinyin');  
  
function sortObjectsByChinesePinyin(array, propertyName) {  
    return array.sort((a, b) => {  
        const aPinyin = pinyin(a[propertyName], { style: pinyin.STYLE_NORMAL, heteronym: false }).join('');  
        const bPinyin = pinyin(b[propertyName], { style: pinyin.STYLE_NORMAL, heteronym: false }).join('');  
        return aPinyin.localeCompare(bPinyin);  
    });  
}  
  
// 示例對(duì)象數(shù)組  
const chineseNames = [  
    { name: '張三', age: 30 },  
    { name: '李四', age: 25 },  
    { name: '王五', age: 35 },  
];  
  
// 使用漢字拼音對(duì)名字進(jìn)行排序  
const sortedChineseNames = sortObjectsByChinesePinyin(chineseNames, 'name');  
  
console.log(sortedChineseNames);

輸出:

[  
  { name: '李四', age: 25 }, // 'lǐ sì'  
  { name: '王五', age: 35 }, // 'wáng wǔ'  
  { name: '張三', age: 30 }  // 'zhāng sān'  
]

強(qiáng)調(diào):如果用VS調(diào)試,別忘記了在luanch.jsion文件中添加  "console": "integratedTerminal",這句話,不然會(huì)報(bào)錯(cuò)。還看不到運(yùn)行結(jié)果。

總結(jié)

到此這篇關(guān)于JS對(duì)象數(shù)組排序方法測(cè)試的文章就介紹到這了,更多相關(guān)JS對(duì)象數(shù)組排序方法內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論