0

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!

4
  • At a first glance. It should sort on a property of the object. But it currently basically compares the object reference. So by what should it sort? Only the Appointment.Id? Commented Feb 4, 2020 at 16:35
  • I just added my comment to sort ascending Commented Feb 4, 2020 at 16:36
  • @LukStorms - the question indicates that it should be date. "How to sort objects in an array by date..." Commented Feb 4, 2020 at 16:37
  • Lol, oops. Yeah, sometimes I just skip the title of the question. :p Commented Feb 4, 2020 at 16:45

3 Answers 3

1

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

}
Sign up to request clarification or add additional context in comments.

Comments

1

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.

bubblesort

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

Comments

0

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

1 Comment

"you don't need the outer loop" - yes you do. Without it, a bubble sort doesn't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.