0

I have the follow issue: I have this JSON response eg:

[{
    "titulo": "Materia",
    "horaInicio": {
        "h": "08",
        "m": "00"
    },
    "horaFin": {
        "h": "10",
        "m": "00"
    }
}, {
    "titulo": "Materia2",
    "horaInicio": {
        "h": "7",
        "m": "00"
    },
    "horaFin": {
        "h": "11",
        "m": "00"
    }
}, {
    "titulo": "Materia3",
    "horaInicio": {
        "h": "11",
        "m": "00"
    },
    "horaFin": {
        "h": "13",
        "m": "00"
    }
}]

and i want to sort this array by "horaInicio" using JavaScript, i looked for some sort function like this one

function predicatBy(prop){
   return function(a,b){
      if( a[prop] > b[prop]){
          return 1;
      }else if( a[prop] < b[prop] ){
          return -1;
      }
      return 0;
   }
}

thats works for 'title' attribute but not for horaInicio neither horaFin.

some solution?

0

4 Answers 4

3

You can sort it with direct access to the properties.

var array = [{ "titulo": "Materia", "horaInicio": { "h": "08", "m": "00" }, "horaFin": { "h": "10", "m": "00" } }, { "titulo": "Materia2", "horaInicio": { "h": "7", "m": "00" }, "horaFin": { "h": "11", "m": "00" } }, { "titulo": "Materia3", "horaInicio": { "h": "11", "m": "00" }, "horaFin": { "h": "13", "m": "00" } }];

array.sort(function (a, b) {
    return a.horaInicio.h - b.horaInicio.h || a.horaInicio.m - b.horaInicio.m
});

document.write('<pre>' + JSON.stringify(array, 0, 4) + '</pre>');

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

Comments

0

Having 'horaInicio' and 'horaFin' as timestamps, instead of separated strings for the hour and minutes, will help you order easily, then you can format the time to a more human readable format, for the final output

Comments

0

// may be it contain typo error

data=JSON.parse(response);

for(i=0;i

{

for(j=i+1;j<data.length;j++)

{

    if(data[i].horalinicio.h==data[j].horalinicio.h)

    {

         if(data[i].horalinicio.m>data[j].horalinicio.m)

         {

          var t=data[j];

          data[j]=data[i];

          data[i]=t;

         }

    }

    else if(data[i].horalinicio.h>data[j].horalinicio.h)

    {    

         var t=data[j];

          data[j]=data[i];

          data[i]=t;


    }

}

}

//make json string

sort_json=JSON.stringify (data);

Comments

0

Just create a comparator function to sort on the start time hour and minutes.

var arr = [{
  "titulo":     "Materia",
  "horaInicio": {"h":"08", "m":"00"},
  "horaFin":    {"h":"10", "m":"00"}
}, {
  "titulo":     "Materia2",
  "horaInicio": {"h":"7",  "m":"00"},
  "horaFin":    {"h":"11", "m":"00"}
}, {
  "titulo":     "Materia3",
  "horaInicio": {"h":"11", "m":"00"},
  "horaFin":    {"h":"13", "m":"00"}
}];

function sortByStartTime(a, b) {
  if (b == null) return -1;
  if (a == null) return 1;
  
  var horaInicioA = a['horaInicio'];
  var horaInicioB = b['horaInicio'];
  
  if (horaInicioB == null) return -1;
  if (horaInicioA == null) return 1;

  var horaDiff = parseInt(horaInicioA['h'], 10) - parseInt(horaInicioB['h'], 10);
  
  if (horaDiff != 0) {
    return horaDiff;
  }

  return parseInt(horaInicioA['m'], 10) - parseInt(horaInicioB['m'], 10);
};

arr.sort(sortByStartTime); // Sort with our custom compartor.

document.body.innerHTML = '<pre>' + JSON.stringify(arr, null, 4) + '</pre>';

You could also convert the times to Date objects and sort them. This sorter will accept a time-grabber function (timeGrabberFn) which will return an object which can be read internally. This makes sorting times for any object array more generic.

var arr = [{
  "titulo":     "Materia",
  "horaInicio": {"h":"08", "m":"00"},
  "horaFin":    {"h":"10", "m":"00"}
}, {
  "titulo":     "Materia2",
  "horaInicio": {"h":"7",  "m":"00"},
  "horaFin":    {"h":"11", "m":"00"}
}, {
  "titulo":     "Materia3",
  "horaInicio": {"h":"11", "m":"00"},
  "horaFin":    {"h":"13", "m":"00"}
}];

function timeSorter(timeGrabberFn) {
  function timeToDateTime(time) {
    return new Date(1970, 0, 1, parseInt(time.hour, 10), parseInt(time.minute, 10), 0, 0);
  }
  function grabDateTime(time) {
    return timeToDateTime(timeGrabberFn(time));
  }
  return function(a, b) {
    return grabDateTime(a) - grabDateTime(b);
  };
}

arr.sort(timeSorter(function(item) {
  return {
    hour   : item.horaInicio.h,
    minute : item.horaInicio.m
  };
}));

document.body.innerHTML = '<pre>' + JSON.stringify(arr, null, 4) + '</pre>';

Comments

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.