繁体   English   中英

如何在不使用 sort 方法的情况下按日期对数组中的对象进行排序?

[英]How to sort objects in an array by date without using the sort method?

对于我必须为学校做的项目,我必须制作一个可以按日期和时间对约会进行排序的应用程序。 我有一个包含对象的数组,但由于日期是嵌套的,因此无法弄清楚如何按日期对其进行排序。

这是我制作的冒泡排序功能:

function bubbleSort() {
    const loop = listOfAppointments.length;

    for(let i = 0; i < loop; i++) {
        for(let j = 0; j < loop; j++) {
            if(listOfAppointments[j] > listOfAppointments[j+1]) {
                let temp = listOfAppointments[j];
                listOfAppointments[j] = listOfAppointments[j+1];
                listOfAppointments[j+1] = temp;
            }
        }
    }
}

这个函数可以很好地处理数字,但我不知道如何使用这个函数对对象进行排序。 我知道javascript中有一个排序功能,但我们不允许使用它。 我尝试排序的数组如下所示:

[
  {
    "Appointment": {
      "Id": 2,
      "nameCustomer": "Henk Jan",
      "addresdCustomer": "somethingstreet 34, middleofnowhere",
      "time": "2020-01-07T10:00:00Z",
      "reason": "gibberish"
    }
  },
  {
    "Appointment": {
      "Id": 1,
      "nameCustomer": "Jan Jaap",
      "addresdCustomer": "somethingpavilion 54, middleofnowhere",
      "time": "2020-01-07T12:15:00Z",
      "reason": "gibberish"
    }
  },
  {
    "Appointment": {
      "Id": 3,
      "nameCustomer": "So Lost",
      "addresdCustomer": "somethingthere 234, middleofnowhere",
      "time": "2020-01-07T11:30:00Z",
      "reason": "gibberish"
    }
  },
  ...
]

谢谢!

尝试这个

function bubbleSort() {
    const loop = listOfAppointments.length;

    for(let i = 0; i < loop; i++) {
        for(let j = i+1; j < loop; j++) {
            if(new Date(listOfAppointments[i].Appointment.time) > new Date(listOfAppointments[j].Appointment.time)) {
                let temp = listOfAppointments[i];
                listOfAppointments[i] = listOfAppointments[j];
                listOfAppointments[j] = temp;
            }
        }
    }

}

从我记得的冒泡排序算法。
它应该是这样的。

时间被转换为日期进行比较。

第二个循环将最高的推到最后。
因此,每次运行时,它都需要少循环 1 个索引。

冒泡排序

 function bubbleSortAppointments(arr) { for(let i = arr.length - 1; i > 0; i--) { for(let j = 0; j < i; j++) { let date1 = new Date(arr[j].Appointment.time); let date2 = new Date(arr[j+1].Appointment.time); if(date1.getTime() > date2.getTime()) { let temp = arr[j]; arr[j] = arr[j+1]; arr[j+1] = temp; } } } } let listOfAppointments = [ { "Appointment": { "Id": 2, "nameCustomer": "Henk Jan", "addressCustomer": "somethingstreet 34, middleofnowhere", "time": "2020-01-07T10:00:00Z", "reason": "gibberish" } }, { "Appointment": { "Id": 1, "nameCustomer": "Jan Jaap", "addressCustomer": "somethingpavilion 54, middleofnowhere", "time": "2020-01-07T12:15:00Z", "reason": "gibberish" } }, { "Appointment": { "Id": 3, "nameCustomer": "So Lost", "addressCustomer": "somethingthere 234, middleofnowhere", "time": "2020-01-07T11:30:00Z", "reason": "gibberish" } } ]; bubbleSortAppointments(listOfAppointments); console.log(listOfAppointments);

首先,您不需要外部循环(计算i变量的循环),因为它根本没有使用。

我建议更改您的bubbleSort函数以接受两个参数:要排序的列表和谓词函数。 谓词函数应该接受两个参数(每个参数都是一个约会)并且它应该返回true / false 使用谓词的结果进行排序:如果谓词返回true则进行排序,否则不进行排序。

下面是一个工作实现。 earliestmostRecent函数是谓词。

注意:显示的bubbleSort函数是 pure ,这意味着它不会修改给定的列表参数,而是创建列表的副本并对副本进行排序。 不必做这样,如果你不想。 但是,我会鼓励你这样做。

 const appointments = [ { "Appointment": { "Id": 2, "nameCustomer": "Henk Jan", "addresdCustomer": "somethingstreet 34, middleofnowhere", "time": "2020-01-07T10:00:00Z", "reason": "gibberish" } }, { "Appointment": { "Id": 1, "nameCustomer": "Jan Jaap", "addresdCustomer": "somethingpavilion 54, middleofnowhere", "time": "2020-01-07T12:15:00Z", "reason": "gibberish" } }, { "Appointment": { "Id": 3, "nameCustomer": "So Lost", "addresdCustomer": "somethingthere 234, middleofnowhere", "time": "2020-01-07T11:30:00Z", "reason": "gibberish" } } ]; // bubbleSort :: Array -> Function -> Array function bubbleSort(list, predicate) { const size = list.length - 1; // <-- or last item will produce errors! const clone = list.slice(); // <-- clone of given list, just to be pure for(let j = 0; j < size; j++) { if(predicate(clone[j], clone[j+1])) { // <-- this line let temp = clone[j]; clone[j] = clone[j+1]; clone[j+1] = temp; } } return clone; } // earliest :: Appointment -> Appointment -> Boolean function earliest (a, b) { return Date.parse(a.Appointment.time) > Date.parse(b.Appointment.time); } // mostRecent :: Appointment -> Appointment -> Boolean function mostRecent (a, b) { return Date.parse(a.Appointment.time) < Date.parse(b.Appointment.time); } console.log('Earliest', bubbleSort(appointments, earliest)) console.log('Most recent', bubbleSort(appointments, mostRecent))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM