简体   繁体   中英

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. 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.

冒泡排序

 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'd suggest to change your bubbleSort function to accept two arguments: A list to sort and a predicate function. The predicate function should take two arguments (each will be an appointment) and it should return true / false . Use the result of the predicate for sorting: If the predicate returns true do a sort, otherwise don't.

Below is a working implementation. The earliest and mostRecent functions are the predicates.

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. 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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