var list = [ {name:'上海',code:'shanghai',}, {name:'西安', code:'xian'}, {name:'深圳', code:'shenzhen'}, {name:'北京', code:'beijing'} ]; list.sort((a,b)=>{ let order=['beijing','xian','shanghai','shenzhen']; return order.indexOf(a.code)-order.indexOf(b.code); }); console.log(list); /* *排序后的结果 [ {name: "北京", code: "beijing"}, {name: "西安", code: "xian"}, {name: "上海", code: "shanghai"} , {name: "深圳", code: "shenzhen"} ] */
转载于csdn:丹哥不是哥
标签:beijing,code,name,javaScript,xian,shanghai,数组,排序,shenzhen From: https://www.cnblogs.com/frenzy/p/17076872.html