Javascript 按ionic3中数组的名字对列表进行排序

Javascript 按ionic3中数组的名字对列表进行排序,javascript,typescript,ionic3,Javascript,Typescript,Ionic3,如何按数组中的firstName对列表进行排序 my.ts文件 initializeItems(){ this.items = [ { avatar: '../../assets/imgs/profile1.jpg', firstName:'Sterlian', lastName:'Victorian', date:new Date().toLocaleDateString(), chat:'Life is beautiful...', component: TetherP

如何按数组中的
firstName
对列表进行排序

my.ts文件

initializeItems(){
    this.items = [
      { avatar: '../../assets/imgs/profile1.jpg', firstName:'Sterlian', lastName:'Victorian',  date:new Date().toLocaleDateString(), chat:'Life is beautiful...', component: TetherPage },
      { avatar: '../../assets/imgs/profile2.jpg', firstName:'Alexis', lastName:' Fournier ', date:'Yesterday', chat:'If you are going to use a passage...', component: TetherPage },
      { avatar: '../../assets/imgs/profile3.jpg', firstName:'Alma', lastName:'  Henry ', date:'10/1/2019', chat:'There are many variations of pass', component: TetherPage },
      { avatar: '../../assets/imgs/profile4.jpg', firstName:'Clara', lastName:' Damian ', date:'Today', chat:'nice to see you after log time ', component: TetherPage },
      { avatar: '../../assets/imgs/profile5.jpg', firstName:'James', lastName:' Charlie ', date:'Yesterday', chat:'Hi!..Have a nice day', component: TetherPage },
      { avatar: '../../assets/imgs/profile6.jpg', firstName:'Ellen', lastName:' Rhystem ', date:'2/1/2019', chat:'It is long establish fact...', component: TetherPage },
      { avatar: '../../assets/imgs/profile7.jpg', firstName:'Irene', lastName:' Reeceem ', date: new Date().toLocaleDateString(), chat:'I think we can all do with a bit more spark', component: TetherPage },
      { avatar: '../../assets/imgs/profile8.jpg', firstName:'Thomas', lastName:' Joe ', date:'Yesterday', chat:'you’re fired up ready to have the best day ever…', component: TetherPage },
      { avatar: '../../assets/imgs/profile9.jpg', firstName:'Charlie', lastName:' Kyle ', date:'3:45 am', chat:'Create the highest, grandest vision possible for your life, because you become what you believe', component: TetherPage },
      { avatar: '../../assets/imgs/profile10.jpg', firstName:'Jacob', lastName:' Henry ', date: new Date().toLocaleDateString(), chat:'Wherever life plants you, bloom with grace', component: TetherPage },
      { avatar: '../../assets/imgs/profile11.jpg', firstName:'Harry', lastName:' Callum ', date:'1/2/2019', chat:'One at a time. Just let your pile of good things grow', component: TetherPage },
      { avatar: '../../assets/imgs/profile12.jpg', firstName:'Oliver', lastName:' Jake ', date: new Date().toLocaleDateString(), chat:'Little by little, day by day, what is mean for you WILL find its way”', component: TetherPage },
      { avatar: '../../assets/imgs/profile13.jpg', firstName:'Jack', lastName:' Connor ', date:'Yesterday', chat:'Learn from yesterday, live for today, hope for tomorrow', component: TetherPage },
      { avatar: '../../assets/imgs/profile14.jpg', firstName:'Julia', lastName:' Margaret ', date: new Date().toLocaleDateString(), chat:'Take time to do what makes your soul happy', component: TetherPage },
      { avatar: '../../assets/imgs/profile15.jpg', firstName:'Jack', lastName:' Tracy ', date:'Today', chat:'Be so happy that when others look at you, they become happy too', component: TetherPage }
    ];
    this.modifiedData = JSON.parse(JSON.stringify(this.items));

}

您可以通过以下方式实现这一目标:

let items = [ {  firstName:'Random', lastName:' Fournier ', date:'Yesterday', chat:'If you are going to use a passage...', }, {  firstName:'Alma', lastName:' Henry ' }, {  firstName:'Clara', lastName:' Damian ' } ];

items = sortByKey(items, 'firstName');

function sortByKey(array, key) {
 return array.sort(function(a, b) {
    var x = a[key]; var y = b[key];
    return ((x < y) ? -1 : ((x > y) ? 1 : 0));
 });
}
let items=[{firstName:'Random',lastName:'Fournier',date:'dayed',chat:'If you will to use a passage…',},{firstName:'Alma',lastName:'Henry'},{firstName:'Clara',lastName:'Damian'}];
items=sortByKey(items,'firstName');
函数sortByKey(数组、键){
返回array.sort(函数(a,b){
变量x=a[key];变量y=b[key];
回报率((xy)?1:0);
});
}

您可以使用
数组对列表进行排序。sort
和使用
字符串的自定义比较函数。localCompare

const sorted = items.sort((a, b) => a.firstName.localeCompare(b.firstName));
请参阅关于和的MDN文档

consttetherpage={};
常数项=[
{avatar:'../../assets/imgs/profile1.jpg',名:'Sterlian',姓:'Victorian',日期:new date().toLocaleDateString(),聊天:'Life is beautife…',组件:TetherPage},
{avatar:'../../assets/imgs/profile2.jpg',名字:'Alexis',姓氏:'Fournier',日期:'date',聊天:'If you will to use a passage…',组件:teterpage},
{avatar:'../../assets/imgs/profile3.jpg',名:'Alma',姓:'Henry',日期:'2019年10月1日',聊天:'pass有许多变体',组件:teterpage},
{avatar:'../../assets/imgs/profile4.jpg',名字:'Clara',姓氏:'Damian',日期:'Today',聊天:'logtime后见到你很高兴',组件:teterpage},
{avatar:'../../assets/imgs/profile5.jpg',名字:'James',姓氏:'Charlie',日期:'date',聊天:'Hi!。祝你有一个愉快的一天',组件:teterpage},
{avatar:'../../assets/imgs/profile6.jpg',名字:'Ellen',姓氏:'Rhystem',日期:'2019年2月1日',聊天:'It's long-set-fact…',组件:TetherPage},
{avatar:'../../assets/imgs/profile7.jpg',名字:'Irene',姓氏:'Reeceem',日期:new date().toLocaleDateString(),聊天:'I think we can don't with the bit more spark',component:TetherPage},
{avatar:'../../assets/imgs/profile8.jpg',名字:'Thomas',姓氏:'Joe',日期:'date',聊天:'you're fired ready to have best day…',component:teterpage},
{阿凡达:'../../assets/imgs/profile9.jpg',名字:'Charlie',姓氏:'Kyle',日期:'3:45 am',聊天:'为你的生活创造最高、最宏伟的愿景,因为你成为你所相信的人',组件:TetherPage},
{avatar:'../../assets/imgs/profile10.jpg',名字:'Jacob',姓氏:'Henry',日期:new date().toLocaleDateString(),聊天:'where life plants you,bloom with grace',component:TetherPage},
{阿凡达:'../../assets/imgs/profile11.jpg',名字:'Harry',姓氏:'Callum',日期:'2019年1月2日',聊天:'一次一个,让你的一堆好东西成长',组件:TetherPage},
{avatar:'../../assets/imgs/profile12.jpg',名字:'Oliver',姓氏:'Jake',日期:new date().toLocaleDateString(),聊天:'Little by Little,day by day,what is means for your WILL find way',组件:teterpage},
{avatar:'../../assets/imgs/profile13.jpg',名字:'Jack',姓氏:'Connor',日期:'date',聊天:'从昨天学习,为今天而活,为明天而活',组件:teterpage},
{avatar:'../../assets/imgs/profile14.jpg',名字:'Julia',姓氏:'Margaret',日期:new date().toLocaleDateString(),聊天:'Take time to do what what your soul soul happy',component:teterpage},
{阿凡达:'../../assets/imgs/profile15.jpg',名字:'Jack',姓氏:'Tracy',日期:'Today',聊天:'开心到别人看着你的时候,他们也会开心',组件:teterpage}
];
const sorted=items.sort((a,b)=>a.firstName.localeCompare(b.firstName));
控制台日志(已排序)