1

Hey please don't shut me down right away because I don't know "the basics" or whatever, if I'm here actually posting it's because I've tried for 2 hours to do this simple thing and I legit can't and need help

I've used the top posts of this thread Javascript - sort objects in an array alphabetically on one property of the array and of a couple more too, it never sorts my array of objects, based on the property I'm pointing out, I don't know if I'm missing something or not this is what my array of objects looks like and I wanna sort them by the horaInicial property, which is a string that keeps an iso 8601

Array(3)
0: Appointment
area: "S. Eter"
data: "2019-05-23T12:40:55.155+01:00"
description: "Sprint CS WEB"
horaFinal: "2019-05-21T11:40:59.028Z"
horaInicial: "2019-05-21T11:40:59.028Z"
id: 17
__proto__: Object
1: Appointment
area: "S. Confiança"
data: "2019-05-23T12:40:55.155+01:00"
description: "AR"
horaFinal: "2019-05-21T16:45:15.448+01:00"
horaInicial: "2019-05-21T16:00:15.448+01:00"
id: 18
__proto__: Object
2: Appointment
area: "djdndjsnsnsnzznj"
data: "2019-05-23T11:18:24.596+01:00"
description: "xbxnxnsnsjsjdjdkssjdjsjsk"
horaFinal: "2019-05-22T10:42:46.770Z"
horaInicial: "2019-05-22T11:41:46.769+01:00"
id: 23
__proto__: Object

then I try sorting it with functions from the other thread, like:

    this.appointments.sort(function(a, b) {
        var textA = a.horaInicial.toUpperCase();
        var textB = b.horaInicial.toUpperCase();
        return (textA < textB) ? -1 : (textA > textB) ? 1 : 0;
     });

    console.log(this.appointments);

the appointments array comes out the same

What exactly am I missing?

sandu
  • 66
  • 10
  • you don't need `toUpperCase` – apple apple May 23 '19 at 16:34
  • Yes I want to do it in Ascending order – sandu May 23 '19 at 16:35
  • I've tried different functions from stack, and none worked, I really don't care what it looks like or what it needs or doesn't need I just wanna get this done, how am I having so much trouble sorting a simple array :\ – sandu May 23 '19 at 16:38
  • the appointments array appears to be sorted to your specification already. Also I noticed your horaInicial values are not all in the same timezone, you can’t string sort them and have it respect the timezone. – James May 23 '19 at 16:41
  • @James nop, horaInicial on 0- 11:40:59 on 1-16:00:15 and on 2-11:41:46, isn't sorted by horaInicial – sandu May 23 '19 at 16:46
  • They are sorted by date. Which is what they are. – James May 23 '19 at 16:59

2 Answers2

0

You are not parsing the Date. This means that what you are doing is comparing strings. Not dates.

In order to use JavaScript sort() function and receive your expected output, you need to parse your dates inside of it. To sort them by your object horaInicial, you can do this:

yourArray.sort(function(a,b){
  return new Date(b.horaInicial) - new Date(a.horaInicial);
});

And this will return a desc array ordered by date, so to convert it to asc you can just use yourArray.reverse().

Look at an example:

let yourArray = [
{
  area: "S. Eter",
  data: "2019-05-23T12:40:55.155+01:00",
  description: "Sprint CS WEB",
  horaFinal: "2019-05-21T11:40:59.028Z",
  horaInicial: "2019-05-21T11:40:59.028Z",
  id: 17
 },
{
  area: "S. Confiança",
  data: "2019-05-23T12:40:55.155+01:00",
  description: "AR",
  horaFinal: "2019-05-21T16:45:15.448+01:00",
  horaInicial: "2019-05-21T16:00:15.448+01:00",
  id: 18
},
{
  area: "djdndjsnsnsnzznj",
  data: "2019-05-23T11:18:24.596+01:00",
  description: "xbxnxnsnsjsjdjdkssjdjsjsk",
  horaFinal: "2019-05-22T10:42:46.770Z",
  horaInicial: "2019-05-22T11:41:46.769+01:00",
  id: 23
}
];

// BEFORE SORTING THE ARRAY
document.write("Before:<br>");
yourArray.forEach((i)=>{
  document.write(new Date(i.horaInicial) + "<br>");
});

//SORT THE ARRAY
yourArray.sort(function(a,b){
  return new Date(b.horaInicial) - new Date(a.horaInicial);
});

// AFTER SORTING THE ARRAY
document.write("<br>After desc:<br>");
yourArray.forEach((i)=>{
  document.write(new Date(i.horaInicial) + "<br>");
});

// REVERSING ARRAY
yourArray.reverse();

// AFTER SORTING AND REVERSING ARRAY
document.write("<br>After asc:<br>");
yourArray.forEach((i)=>{
  document.write(new Date(i.horaInicial) + "<br>");
});
k3llydev
  • 2,057
  • 2
  • 11
  • 21
0

Why would you sort timestamps using alphanumeric comparison? Convert them into epoch timestamps and then sort using the resulting numbers.

1.Convert the timestamps into epoch -

 arr2 = arr.map(item => {
    item.horaInicialEpoch = new Date(item.horaInicial).getTime();
    return item; 
 })
  1. Sort based on horaInicialEpoch

    arr2.sort((a,b) => a.horaInicialEpoch - b.horaInicialEpoch)

P.S Only use string representations of time when you need to present it for human consumption. When programatically using datetime, use the seconds since epoch.

suv
  • 1,373
  • 1
  • 10
  • 18