简体   繁体   English

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

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

For a project I have to do for school, I have to make an application that can sort appointments by date and time.对于我必须为学校做的项目,我必须制作一个可以按日期和时间对约会进行排序的应用程序。 I have an array with objects in them but can't figure out how to sort it by date as the date is nested.我有一个包含对象的数组,但由于日期是嵌套的,因此无法弄清楚如何按日期对其进行排序。

Here is the bubblesort function I made:这是我制作的冒泡排序功能:

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;
            }
        }
    }
}

This function works fine with numbers, but I can't figure out how to sort the object using this function.这个函数可以很好地处理数字,但我不知道如何使用这个函数对对象进行排序。 I know there is a sort function in javascript, but we are not allowed to use it.我知道javascript中有一个排序功能,但我们不允许使用它。 The array I'm trying to sort looks like this:我尝试排序的数组如下所示:

[
  {
    "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"
    }
  },
  ...
]

Thanks!谢谢!

Try This尝试这个

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;
            }
        }
    }

}

From what I remember from the bubblesort algorithm.从我记得的冒泡排序算法。
It should be something like this.它应该是这样的。

The times are converted to dates for comparison.时间被转换为日期进行比较。

And the 2nd loop pushes the highest to the end.第二个循环将最高的推到最后。
So it with each run it needs to loop 1 less index.因此,每次运行时,它都需要少循环 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);

First off, you don't need the outer loop (the one that counts the i variable), because it isn't used at all.首先,您不需要外部循环(计算i变量的循环),因为它根本没有使用。

I'd suggest to change your bubbleSort function to accept two arguments: A list to sort and a predicate function.我建议更改您的bubbleSort函数以接受两个参数:要排序的列表和谓词函数。 The predicate function should take two arguments (each will be an appointment) and it should return true / false .谓词函数应该接受两个参数(每个参数都是一个约会)并且它应该返回true / false Use the result of the predicate for sorting: If the predicate returns true do a sort, otherwise don't.使用谓词的结果进行排序:如果谓词返回true则进行排序,否则不进行排序。

Below is a working implementation.下面是一个工作实现。 The earliest and mostRecent functions are the predicates. earliestmostRecent函数是谓词。

Note: The bubbleSort function shown is pure , which means instead of modifying the given list argument, it creates a clone of list and sorts that clone.注意:显示的bubbleSort函数是 pure ,这意味着它不会修改给定的列表参数,而是创建列表的副本并对副本进行排序。 You don't have to do it this way if you don't want to.不必做这样,如果你不想。 However, I would encourage you to do it.但是,我会鼓励你这样做。

 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