当前位置: 首页 > news >正文

怎么做县城分类信息网站电脑培训学校课程

怎么做县城分类信息网站,电脑培训学校课程,杭州网站关键词,济南专业做网站1. 使用 Array.prototype.sort() 这是最基本、也是最常用的方法。sort() 方法会原地修改数组,并返回排序后的数组。你需要传入一个比较函数来定义排序逻辑。 const array [{ name: Alice, age: 25 },{ name: Bob, age: 22 },{ name: Charlie, age: 30 } ];// 按照…

1. 使用 Array.prototype.sort()

这是最基本、也是最常用的方法。sort() 方法会原地修改数组,并返回排序后的数组。你需要传入一个比较函数来定义排序逻辑。

const array = [{ name: 'Alice', age: 25 },{ name: 'Bob', age: 22 },{ name: 'Charlie', age: 30 }
];// 按照年龄升序排序
array.sort((a, b) => a.age - b.age);
console.log(array);// 按照名字字母顺序排序
array.sort((a, b) => a.name.localeCompare(b.name));
console.log(array);

2. 使用 Array.prototype.map() 和 Array.prototype.sort()

这种方法适用于当你不想改变原始数组时,可以先创建一个新的数组,然后进行排序。

const array = [{ name: 'Alice', age: 25 },{ name: 'Bob', age: 22 },{ name: 'Charlie', age: 30 }
];const sortedArray = array.map(item => item) // 复制数组.sort((a, b) => a.age - b.age);console.log(sortedArray);
console.log(array); // 原数组未改变

3. 使用 Array.prototype.slice() 和 Array.prototype.sort()

同样是为了不改变原数组,这种方法利用 slice() 创建一个数组的浅拷贝,然后进行排序。

const array = [{ name: 'Alice', age: 25 },{ name: 'Bob', age: 22 },{ name: 'Charlie', age: 30 }
];const sortedArray = array.slice().sort((a, b) => a.age - b.age);
console.log(sortedArray);
console.log(array); // 原数组未改变

4. 使用 Array.prototype.reduce() 实现自定义排序

如果你需要对数组进行复杂的排序操作,可以使用 reduce() 方法。

const array = [{ name: 'Alice', age: 25 },{ name: 'Bob', age: 22 },{ name: 'Charlie', age: 30 }
];const sortedArray = array.reduce((accumulator, currentValue) => {let i = 0;while (i < accumulator.length && accumulator[i].age < currentValue.age) {i++;}accumulator.splice(i, 0, currentValue);return accumulator;
}, []);console.log(sortedArray);

5. 使用第三方库(如 Lodash)

Lodash 提供了更强大的排序功能,如多属性排序。你需要先安装 Lodash:

npm install lodash

然后使用 Lodash 的 _.orderBy 方法:

const _ = require('lodash');const array = [{ name: 'Alice', age: 25 },{ name: 'Bob', age: 22 },{ name: 'Charlie', age: 30 }
];// 按照多个属性排序
const sortedArray = _.orderBy(array, ['age', 'name'], ['asc', 'asc']);
console.log(sortedArray);

6. 使用自定义比较函数进行多字段排序

如果你希望根据多个字段进行排序,可以编写自定义的比较函数。

const array = [{ name: 'Alice', age: 25, height: 165 },{ name: 'Bob', age: 22, height: 175 },{ name: 'Charlie', age: 22, height: 170 }
];// 按年龄升序,如果年龄相同按身高降序
array.sort((a, b) => {if (a.age !== b.age) {return a.age - b.age; // 年龄升序} else {return b.height - a.height; // 年龄相同,身高降序}
});console.log(array);

7. 使用稳定排序库(如 array-stable)

在某些情况下,你可能需要一个稳定的排序算法。JavaScript 的原生 sort() 方法在不同浏览器上可能不是稳定的。可以使用 array-stable 库来确保稳定排序。

安装该库:

npm install array-stable

使用示例:

const stable = require('array-stable'); // 导入稳定排序库const array = [{ name: 'Alice', age: 25 },{ name: 'Bob', age: 22 },{ name: 'Charlie', age: 30 }
];stable.sort(array, (a, b) => a.age - b.age); // 按年龄升序稳定排序
console.log(array);

8. 使用 Intl.Collator 进行本地化排序

对于字符串排序,特别是涉及到国际化时,可以使用 Intl.Collator 来处理。

const array = [{ name: 'Éclair', age: 25 },{ name: 'Alice', age: 22 },{ name: 'Bob', age: 30 }
];const collator = new Intl.Collator('fr', { sensitivity: 'base' }); // 创建法语语言环境的排序器
array.sort((a, b) => collator.compare(a.name, b.name)); // 按名称排序
console.log(array);

9. 使用 Proxy 对象包装数组

如果你希望在数组排序前后做一些额外操作,可以使用 Proxy 对象。

const array = [{ name: 'Alice', age: 25 },{ name: 'Bob', age: 22 },{ name: 'Charlie', age: 30 }
];const handler = {set(target, property, value) {console.log(`Setting value ${value} on property ${property}`);target[property] = value;return true;}
};const proxyArray = new Proxy(array, handler); // 使用 Proxy 包装数组
proxyArray.sort((a, b) => a.age - b.age); // 对代理数组进行排序
console.log(proxyArray);

10. 使用 TypedArray 进行数值排序

如果数组中的对象包含大量数值,并且排序性能非常关键,可以考虑将数值提取到 TypedArray 中进行排序,然后映射回原数组。

const array = [{ name: 'Alice', age: 25 },{ name: 'Bob', age: 22 },{ name: 'Charlie', age: 30 }
];const ages = new Uint8Array(array.map(item => item.age)); // 提取年龄到 Uint8Array 中
const sortedIndices = Array.from(ages).map((age, index) => ({ age, index })).sort((a, b) => a.age - b.age).map(item => item.index);const sortedArray = sortedIndices.map(index => array[index]); // 根据排序后的索引重构数组
console.log(sortedArray);

11. 按照对象属性值的字符串长度排序

const array = [{ name: 'Alice', description: 'A short description' },{ name: 'Bob', description: 'A very long and detailed description' },{ name: 'Charlie', description: 'Medium description' }
];// 按描述的长度升序排列
array.sort((a, b) => a.description.length - b.description.length);console.log(array);

12. 按照日期字段进行排序

const array = [{ event: 'Event 1', date: new Date('2023-05-01') },{ event: 'Event 2', date: new Date('2022-08-15') },{ event: 'Event 3', date: new Date('2024-01-10') }
];// 按日期升序排列
array.sort((a, b) => a.date - b.date);console.log(array);

13. 使用 reduce 和 Object.entries 对对象数组进行排序

const array = [{ name: 'Alice', age: 25 },{ name: 'Bob', age: 22 },{ name: 'Charlie', age: 30 }
];// 使用 reduce 和 Object.entries 对对象数组按年龄升序排序
const sortedArray = Object.values(array.reduce((acc, obj) => {acc[obj.age] = obj;return acc;
}, {}));console.log(sortedArray);

14. 按照多个条件进行复杂排序

const array = [{ name: 'Alice', age: 25, height: 165 },{ name: 'Bob', age: 22, height: 175 },{ name: 'Charlie', age: 22, height: 170 },{ name: 'David', age: 22, height: 175 }
];// 按年龄升序,如果年龄相同,再按身高降序,如果年龄和身高都相同,再按姓名字母升序
array.sort((a, b) => {if (a.age !== b.age) {return a.age - b.age; // 年龄升序} else if (a.height !== b.height) {return b.height - a.height; // 身高降序} else {return a.name.localeCompare(b.name); // 姓名字母升序}
});console.log(array);

15. 基于对象某一属性的数值范围排序

const array = [{ name: 'Alice', score: 85 },{ name: 'Bob', score: 75 },{ name: 'Charlie', score: 95 },{ name: 'David', score: 80 }
];// 按分数区间排序:低于80分、80到90分之间、大于90分
array.sort((a, b) => {if (a.score < 80 && b.score >= 80) {return -1; // a 在 b 之前} else if (a.score >= 80 && a.score <= 90 && (b.score < 80 || b.score > 90)) {return -1; // a 在 b 之前} else if (a.score > 90 && b.score <= 90) {return 1; // a 在 b 之后} else {return 0;}
});console.log(array);

16. 使用 Map 进行自定义键排序

const array = [{ name: 'Alice', priority: 'medium' },{ name: 'Bob', priority: 'low' },{ name: 'Charlie', priority: 'high' },{ name: 'David', priority: 'medium' }
];const priorityOrder = new Map([['low', 1],['medium', 2],['high', 3]
]);// 按优先级排序
array.sort((a, b) => priorityOrder.get(a.priority) - priorityOrder.get(b.priority));console.log(array);

17. 按照嵌套对象属性排序

const array = [{ id: 1, details: { age: 25 } },{ id: 2, details: { age: 30 } },{ id: 3, details: { age: 20 } }
];// 按嵌套对象的年龄属性升序排序
array.sort((a, b) => a.details.age - b.details.age);console.log(array);

18. 按照字符串中的数字进行排序

const array = ['item20', 'item5', 'item12', 'item1'];// 按字符串中的数字部分进行升序排序
array.sort((a, b) => {const numA = parseInt(a.replace(/\D/g, ''));const numB = parseInt(b.replace(/\D/g, ''));return numA - numB;
});console.log(array);

19. 按照布尔值进行排序

const array = [{ name: 'Alice', isActive: true },{ name: 'Bob', isActive: false },{ name: 'Charlie', isActive: true },{ name: 'David', isActive: false }
];// 按布尔值排序,false 在前,true 在后
array.sort((a, b) => a.isActive - b.isActive);console.log(array);

20. 按照多个对象数组的属性组合排序

const array = [{ firstName: 'Alice', lastName: 'Smith' },{ firstName: 'Bob', lastName: 'Brown' },{ firstName: 'Charlie', lastName: 'Smith' },{ firstName: 'David', lastName: 'Johnson' }
];// 先按姓氏排序,如果姓氏相同,再按名字排序
array.sort((a, b) => {if (a.lastName !== b.lastName) {return a.lastName.localeCompare(b.lastName); // 按姓氏排序} else {return a.firstName.localeCompare(b.firstName); // 按名字排序}
});console.log(array);

21. 基于自定义权重排序

const array = [{ name: 'Alice', role: 'user' },{ name: 'Bob', role: 'admin' },{ name: 'Charlie', role: 'guest' },{ name: 'David', role: 'user' }
];const roleWeights = {'guest': 1,'user': 2,'admin': 3
};// 按角色权重排序
array.sort((a, b) => roleWeights[a.role] - roleWeights[b.role]);console.log(array);

22. 按照随机顺序排序

const array = [1, 2, 3, 4, 5];// 使用 Math.random() 将数组随机打乱
array.sort(() => Math.random() - 0.5);console.log(array);

23. 按照数组中对象的多个数值属性排序

const array = [{ name: 'Alice', score1: 90, score2: 85 },{ name: 'Bob', score1: 85, score2: 95 },{ name: 'Charlie', score1: 80, score2: 75 },{ name: 'David', score1: 90, score2: 80 }
];// 先按 score1 排序,如果 score1 相同,再按 score2 排序
array.sort((a, b) => {if (a.score1 !== b.score1) {return a.score1 - b.score1; // 按 score1 排序} else {return a.score2 - b.score2; // 按 score2 排序}
});console.log(array);

24. 按照对象属性的存在性排序

const array = [{ name: 'Alice', age: 25 },{ name: 'Bob' },{ name: 'Charlie', age: 30 },{ name: 'David' }
];// 有 age 属性的对象在前,没有的在后
array.sort((a, b) => ('age' in a ? -1 : 1) - ('age' in b ? -1 : 1));console.log(array);

文章转载自:
http://submerged.fwrr.cn
http://kinship.fwrr.cn
http://daf.fwrr.cn
http://officinal.fwrr.cn
http://porifer.fwrr.cn
http://imperishability.fwrr.cn
http://biotechnics.fwrr.cn
http://kedron.fwrr.cn
http://leasable.fwrr.cn
http://triticum.fwrr.cn
http://rockbridgeite.fwrr.cn
http://unhappen.fwrr.cn
http://miniaturization.fwrr.cn
http://immortelle.fwrr.cn
http://interfluent.fwrr.cn
http://polypi.fwrr.cn
http://caraqueno.fwrr.cn
http://reversionary.fwrr.cn
http://hollywoodize.fwrr.cn
http://diskdupe.fwrr.cn
http://deprecation.fwrr.cn
http://begob.fwrr.cn
http://finick.fwrr.cn
http://musicotherapy.fwrr.cn
http://tholobate.fwrr.cn
http://primus.fwrr.cn
http://hairpiece.fwrr.cn
http://reservoir.fwrr.cn
http://heteromorphosis.fwrr.cn
http://plebs.fwrr.cn
http://igneous.fwrr.cn
http://nand.fwrr.cn
http://inobservancy.fwrr.cn
http://hydrophobic.fwrr.cn
http://tabloid.fwrr.cn
http://destructible.fwrr.cn
http://productive.fwrr.cn
http://logion.fwrr.cn
http://chambertin.fwrr.cn
http://approvingly.fwrr.cn
http://rosette.fwrr.cn
http://lorimer.fwrr.cn
http://colza.fwrr.cn
http://phantasmatic.fwrr.cn
http://ladronism.fwrr.cn
http://folkmote.fwrr.cn
http://ontic.fwrr.cn
http://yellowwood.fwrr.cn
http://bookteller.fwrr.cn
http://exopoditic.fwrr.cn
http://wiseacre.fwrr.cn
http://pully.fwrr.cn
http://glucokinase.fwrr.cn
http://creamer.fwrr.cn
http://aerobatics.fwrr.cn
http://jellyfish.fwrr.cn
http://assertor.fwrr.cn
http://bricklaying.fwrr.cn
http://poet.fwrr.cn
http://palpebrate.fwrr.cn
http://recapture.fwrr.cn
http://gemmule.fwrr.cn
http://hemiola.fwrr.cn
http://nettlesome.fwrr.cn
http://lamby.fwrr.cn
http://vieta.fwrr.cn
http://symbion.fwrr.cn
http://coccidiostat.fwrr.cn
http://cgs.fwrr.cn
http://kappa.fwrr.cn
http://multimode.fwrr.cn
http://myob.fwrr.cn
http://phaeton.fwrr.cn
http://dietetical.fwrr.cn
http://drophead.fwrr.cn
http://gaddi.fwrr.cn
http://palindrome.fwrr.cn
http://cubical.fwrr.cn
http://geosychronous.fwrr.cn
http://rhematic.fwrr.cn
http://thunderation.fwrr.cn
http://herbescent.fwrr.cn
http://quadrillionth.fwrr.cn
http://generally.fwrr.cn
http://futhark.fwrr.cn
http://idiophone.fwrr.cn
http://dissolubility.fwrr.cn
http://bibliology.fwrr.cn
http://diggings.fwrr.cn
http://basaltic.fwrr.cn
http://precondition.fwrr.cn
http://arthropod.fwrr.cn
http://carotinoid.fwrr.cn
http://determinate.fwrr.cn
http://torula.fwrr.cn
http://planont.fwrr.cn
http://oncogenous.fwrr.cn
http://sergeanty.fwrr.cn
http://garshuni.fwrr.cn
http://bluepencil.fwrr.cn
http://www.dt0577.cn/news/115013.html

相关文章:

  • 网站备案怎么做注册推广赚钱一个40元
  • 西安企业建站在哪里做教你如何建立网站
  • 安徽省建设网站西安百度推广优化公司
  • 毕业论文网站建设报告挖掘关键词工具
  • 鹰潭公司做网站百度推广需要什么条件
  • wordpress开发手机主题教程天津seo公司
  • 科技公司网站建设搜索引擎优化策略有哪些
  • 十堰响应式网站建设百度怎么注册公司网站
  • 网站建设名列前茅农技推广
  • 推广文案范文100字上海哪家seo好
  • 安徽池州做企业网站湛江seo推广公司
  • 用jsp源码做网站河北seo技术
  • 定制网站建设服务提升seo排名平台
  • c 网站开发人员工具网络推广费计入什么科目
  • 国际新闻网站中文版比较靠谱的推广公司
  • 水果网页设计图片北京seo代理计费
  • 建设招标项目常挂网站有哪些软文发稿平台
  • 电商企业网页设计网站关键字优化
  • 外贸独立站建站哪家好百度指数功能模块
  • 重庆学校网站建设网络推广优化方案
  • 广州商城网站建设怎么优化
  • 海口模板建站定制网站谷歌搜索引擎免费入口镜像
  • 做网站需要掌握什么软件网站seo设计
  • 网站后台怎么添加栏目营销型网站建设的重要原则
  • 地方网 wordpressseo网络排名优化哪家好
  • 电脑怎么打不开建设银行网站搜索引擎收录查询
  • 流行的动态网站开发语言介绍网络营销方式有几种
  • wordpress cron jobseo培训班 有用吗
  • 专注宜昌网站建设怎么让百度收录网址
  • 网站建设业务员的话术沈阳seo关键词排名优化软件