项目中经常会遇到要对数组中的对象进行排序的情况, 这类排序大致分为三种情况。 根据数字、字母或汉字进行排排序。 这三类排序需要分别进行处理。 核心代码
- 数字
item.sort((a,b) => a.number - b.number) - 字母
方法1:item.sort((a,b) => a.name.localeCompare(b.name))
方法2: 这种方法只比较了第一个字母, 建议还是使用方法1, 跟比较汉字的方法一致。item.sort((a,b) => a.name.charCodeAt(0)-b.name.charCodeAt(0))。 - 汉字
item.sort(a,b) => a.name.localeCompare(b.name))
1. 数组对象,根据不同的类型进行排序
customSort() {
let item = [
{
name: "a",
miniName: "爱a是你我",
number: 1
},
{
name: "d",
miniName: "曹c操快点跑",
number: 33
},
{
name: "c",
miniName: "必b须好好努力",
number: 4
},
{
name: "b",
miniName: "大d哥小心点",
number: 2
}
];
function sortByParams(str) {
// 判断数组中str属性属于数字,字母还是汉字
let whichType = "";
item.forEach(item => {
if (typeof item[str] === "number") {
whichType = "number";
} else if (/^[\u4e00-\u9fa5]*$/.test(item[str].charAt(0))){
whichType = "characters";
} else if (typeof item[str] === "string") {
whichType = "string";
}
});
// 根据是数字字母或汉字进行不同的排序操作
if (whichType === "number") {
console.log("进入whichType number");
return (a, b) => a[str] - b[str];
} else if (whichType === "characters") {
console.log("进入whichType characters");
return (a, b) => a[str].localeCompare(b[str]);
} else if (whichType === "string") {
console.log("进入whichType string");
return (a, b) => a[str].charCodeAt(0) - b[str].charCodeAt(0);
}
}
item.sort(sortByParams("name"));
// item.sort(sortByParams("number"));
// item.sort(sortByParams("miniName"));
console.log(item);
}
上边那种写法平常使用不是很方便, 所以我改进了一下。 直接调用函数, 第一个参数传递要排序的数组对象, 第二个参数传递要排序的项。 返回的数据就是根据第二个参数去排序的数组对象。
# 这是item参数, 可以根据name(字符串), miniName(中文), number(数字)分别进行排序
item: [
{
name: "a",
miniName: "爱a是你我",
number: 1
},
{
name: "d",
miniName: "曹c操快点跑",
number: 11
},
{
name: "da",
miniName: "曹d操快点跑",
number: 12
},
{
name: "c",
miniName: "必b须好好努力",
number: 4
},
{
name: "b",
miniName: "大d哥小心点",
number: 2
}
]
====================================================================================================
customSort(arr, str) {
let item = arr.slice();
// 判断数组中str属性属于数字,字母还是汉字
let whichType = "";
item.forEach(item => {
if (typeof item[str] === "number") {
whichType = "number";
} else if (/^[\u4e00-\u9fa5]*$/.test(item[str].charAt(0))) {
whichType = "characters";
} else if (typeof item[str] === "string") {
whichType = "string";
}
});
// 根据是数字字母或汉字进行不同的排序操作
if (whichType === "number") {
console.log("进入whichType number");
return item.sort((a, b) => a[str] - b[str]);
} else if (whichType === "characters") {
console.log("进入whichType characters");
return item.sort((a, b) => a[str].localeCompare(b[str]));
} else if (whichType === "string") {
console.log("进入whichType string");
return item.sort((a, b) => a[str].charCodeAt(0) - b[str].charCodeAt(0));
}
}
# 使用举例子
let res = this.customSort(this.item, "miniName");
let res2 = this.customSort(this.item, "name");
let res3 = this.customSort(this.item, "number");
2.数组对象中根据姓名按照数字,字母,中文的顺序排序,中文部分按照首字母拼音排序
问题: 将下面这个数组对象, 以
name为标准。按照数字,字母, 汉字的顺序进行排序。 数字,从小到大。 字母从a到z, 中文按照首字母拼音排序
customSort() {
let item = [
{ name: 111, first: "andy凌云1" },
{ name: 32, first: "andy凌云2" },
{ name: 12, first: "andy凌云3" },
{ name: 1, first: "andy凌云12" },
{ name: "66", first: "andy凌云333" },
{ name: "wuhaha", first: "andy凌222" },
{ name: "余喝", first: "andy凌云34" },
{ name: "12", first: "andy凌云44" },
{ name: "安安", first: "andy凌云111" },
{ name: "大伯", first: "andy凌云1" },
{ name: "zhaha", first: "andy凌云1" }
];
解题思路: 将数组进行循环, 根据
item.name去判断数字, 字母和汉字。 分别push进三个不同种类的空数组中, 然后分别对这三类数组进行排序。 最后使用concat去拼接三个排好序的数组
完整代码如下
customSort() {
let item = [
{ name: 111, first: "andy凌云1" },
{ name: 32, first: "andy凌云2" },
{ name: 12, first: "andy凌云3" },
{ name: 1, first: "andy凌云12" },
{ name: "66", first: "andy凌云333" },
{ name: "wuhaha", first: "andy凌222" },
{ name: "余喝", first: "andy凌云34" },
{ name: "12", first: "andy凌云44" },
{ name: "安安", first: "andy凌云111" },
{ name: "大伯", first: "andy凌云1" },
{ name: "zhaha", first: "andy凌云1" }
];
let chineseChars = [];
let chars = [];
let list = [];
let numbers = [];
function dataforEach(item) {
// 判断是否为中文
item.forEach(item => {
if (
typeof item.name === "string" &&
/^[\u4e00-\u9fa5]*$/.test(item.name.charAt(0))
) {
chineseChars.push(item); // 姓名首字符为中文的
} else if (
typeof item.name === "string" &&
!/^[\u4e00-\u9fa5]*$/.test(item.name.charAt(0))
) {
chars.push(item); // 姓名首字符非中文的(字母,数字)
} else if (typeof item.name === "number") {
numbers.push(item);
}
});
}
dataforEach(item);
chars.sort((a, b) => a.name.charCodeAt(0) - b.name.charCodeAt(0));
numbers.sort((a, b) => a.name - b.name);
chineseChars.sort((a, b) => a.name.localeCompare(b.name));
list = numbers.concat(chars, chineseChars); // list为最终想要的数组
console.log(list);
}
}