按特定值快速排序功能数组 [英] quick Sortfunction Array by specific value

查看:40
本文介绍了按特定值快速排序功能数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

完整代码,位于

我有这个循环

  for(让i = 0; i< ultraarr.length; i ++){ultraarr.sort();$(.row").append(ultraarr [i] .display())} 

这将调用函数显示,它是一个类函数,它基本上将数据插入到html中.我希望它做什么:

我希望它按照日期升序排列.确实研究了a-b的东西,但看不到我的解决方法

信息类的功能:

  display(){让内容=`< div class ="col-sm-12 col-md-6 col-lg-3">< div class ="card">< img class ="card-img-top d-sm-none d-md-block" src ="img/$ {this.img}" alt ="$ {this.name}">< div class ="card-body">< h5 class ="card-title"> $ {this.name}</h5>< p class ="card-text">城市:$ {this.city}邮政编码$ {this.zip}< br><标签>事件信息</label>< ul>< li> $ {this.edate}</li>< li> $ {this.etime}</li>< li> $ {this.eprice}</li>< li> $ {this.eweb}</li></ul>< p>已创建:$ {this.dates.toLocaleString('de-AT')}</p></div></div></div>`;返回内容;} 

在日期传递给他们创建的类构造函数之前,请按如下所示:新日期(2011、1、1、3、25)当我控制台登录例如typeof(newClassItem.dates)时,它说的是对象,而不是数字或字符串,所以我真的不知道该怎么做-我想可能是.valueOf吗?

解决方案

保存日期时,请使用Date对象上的toJSON methond保存日期.将sort函数传递给数组sort函数.

  var array = [{'name':'test1','date':'2020-06-19T14:21:31.633Z'},{'name':'test2','date':'2020-07-30T12:22:53.113Z'},]函数sortDate(rec1,rec2){返回新的Date(rec1.date)<新的Date(rec2.date)?-1:1}array.sort(sortDate)console.log(array) 

这将以升序对数组进行排序.如果要按降序排序,请将比较符号更改为'>'或将-1和1交换.

如果数组内部将日期字段存储为日期数据类型,则可以执行以下操作

 函数sortDate(rec1,rec2){返回rec1.date<rec2.date?-1:1}ultraarr.sort(sortDate) 

Full Code at https://codepen.io/CodeLegend27/pen/ExPZRxa

did read similary articles but didnt managed to solve it

so my array looks like this:

I have this loop

for (let i = 0; i < ultraarr.length; i++) {
  ultraarr.sort();
  $(".row").append(ultraarr[i].display())
}

this calls the function display which is a class function and it basically inserts the data into html. What i want it to DO:

i want it ordered according to the dates ascending. did research the a-b stuff but cant see the solution for me

CLASS FUNCTION for your INFO:

 display() {
    let content =
      `
      <div class="col-sm-12 col-md-6 col-lg-3">
      <div class="card">
      <img class="card-img-top d-sm-none d-md-block" src="img/${this.img}" alt="${this.name}">
      <div class="card-body">
        <h5 class="card-title">${this.name}</h5>
        <p class="card-text">City: ${this.city} Zip-Code ${this.zip}
        <br>
        <label> Event Infos </label>
        <ul>
        <li>${this.edate}</li>
        <li>${this.etime}</li>
        <li>${this.eprice}</li>
        <li>${this.eweb}</li>
        </ul>
        <p>Created: ${this.dates.toLocaleString('de-AT')}
        </p>
            </div></div>
    </div>
      ` ;
    return content;
  }

btw before the dates are passed to the class constructor they created like this: new Date(2011, 1, 1, 3, 25), and when i console log for example typeof(newClassItem.dates) it says object and not number or string so i dont really know how to do it - i thought maybe with .valueOf?

解决方案

When you are saving the dates, save them using toJSON methond on the Date Object. Pass the sort function to the array sort function.

var array = [
  {'name':'test1','date':'2020-06-19T14:21:31.633Z'},
  {'name':'test2','date':'2020-07-30T12:22:53.113Z'},
]

function sortDate(rec1,rec2){
  return new Date(rec1.date) < new Date(rec2.date) ? -1 : 1
}

array.sort(sortDate)

console.log(array)

This is will sort the array in ascending order. If you want to sort it in descending order, either change the comparison sign to '>' or swap -1 and 1.

If the array internal is storing date field as dates data type, you can do the following

function sortDate(rec1,rec2){
  return rec1.date < rec2.date ? -1 : 1
}

ultraarr.sort(sortDate)

这篇关于按特定值快速排序功能数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆