Sort array by user variables

$("#show_times_available_" + pos).find("td span").parent().each(function(){ 
    array.push($(this).attr("id")); 
    array = array.sort();
});

My array takes all the elements and grabs their identifier and pops it into the array, so my array will look like:

[ "mon_0600-0", "sun_0700-0", "thu_0600-0", "thu_0700-0", "tue_0300-0", "wed_0700-0" ];

What I'm trying to do is sort these elements as (mon, tue, wed, etc.) every time a new element is inserted into the array. so my array will end up like:

[ "mon_0600-0", "tue_0300-0", "wed_0700-0", "thu_0600-0", "thu_0700-0", "sun_0700-0" ];

Using the base function sort()will be placed in alphabetical order, and I know that a function sort()can take another function. I'm just not sure how to set this in javascript javascript or jQuery. Is there a way to CASE THEN or use when()and then()to order them? I am looking for everything around google and SO, but nothing.

+4
6

// your array
array = [ "mon_0600-0", "sun_0700-0", "thu_0600-0", "thu_0700-0", "tue_0300-0", "wed_0700-0" ];

// the order stored in an object
daynames={
    mon:1,
    tue:2,
    wed:3,
    thu:4,
    fri:5,
    sat:6,
    sun:7
}

// the custom sort function which gets the value for the dayname-key and compares
function SortByDayName(a, b){

  // get the value of dayname + the time 
  var aName = daynames[a.substr(0,3)] + a.substr(4,7);;
  var bName = daynames[b.substr(0,3)] + b.substr(4,7);; 
  return ((aName < bName) ? -1 : ((aName > bName) ? 1 : 0));
}

// apply sort
array.sort(SortByDayName);

// output:
["mon_0600-0", "tue_0300-0", "wed_0700-0", "thu_0600-0", "thu_0700-0", "sun_0700-0"]
+1

sort(), . , , ( ):

var arr = ["mon_0600-0", "sun_0700-0", "thu_0600-0", "thu_0700-0", "tue_0300-0", "wed_0700-0"];

// store the order of the days
var days = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];

var arrSorted = arr.sort(function(a, b) {
  // a and b are two times to compare

  // split each into days and times, e.g ["mon", "0600-0"]
  var aSplit = a.split("_");
  var bSplit = b.split("_");
  
  // get the index of the days to compare
  var dayA = days.indexOf(aSplit[0]);
  var dayB = days.indexOf(bSplit[0]);
  
  // if days are the same, compare the times using normal String comparison
  // if days are different, return the comparison of their position in the array
  return dayA == dayB ?
  	aSplit[1].localeCompare(bSplit[1])
  	: dayA - dayB;
});

console.log(arrSorted);
+2

. sort, , .

, compare(a,b), , a, , , greate than zero, b a.

var array = ["X123", "X0072", "X04"];

var compareFunction = function(a,b) {
  var _a = parseInt(a.substring(1));
  var _b = parseInt(b.substring(1));
  return _a-_b;
}

$('#original').text(JSON.stringify(array));

array.sort();
$('#sort').text(JSON.stringify(array));

array.sort(compareFunction);
$('#customSort').text(JSON.stringify(array));
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<p>Original:</p>
<pre id="original"></pre>

<p>Sort:</p>
<pre id="sort"></pre>

<p>Custom sort:</p>
<pre id="customSort"></pre>
+1

, :

var days = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];

:

array = array.sort(function (x, y) {
    return days.indexOf(x.slice(0,3)) - days.indexOf(y.slice(0,3));
});
+1

,

var array = ["mon_0600-0", "sun_0700-0", "thu_0600-0", "thu_0700-0", "tue_0300-0", "wed_0700-0"],
  sort = ["mon", "tue", "wed", "thu", "fri", "sat", "sun"];

var sortondays = function() {
  var newArray = [];
  for (i = 0; i < sort.length; i++) {
    var t = sort[i];
    for (x = 0; x < array.length; x++) {
      if (array[x].startsWith(t)) {
        newArray.push(array[x])
      }
    }
  }
  return newArray;
}
console.log(sortondays());
+1

This problem is ideal for Sorting with a map , since splitting and searching per day is only done once for an element, and not for each kind of Callback.

// the array to be sorted
var data = ["mon_0600-0", "sun_0700-0", "thu_0600-0", "thu_0700-0", "tue_0300-0", "wed_0700-0"];

// temporary array holds objects with position and sort-value
var mapped = data.map(function (el, i) {
    var value = el.split('_'), day;
    return {
        index: i,
        day: { mon: 1, tue: 2, wed: 3, thu: 4, fri: 5, sat: 6, sun: 7 }[value[0]] || 0,
        hour: value[1]
    };
})

// sorting the mapped array containing the reduced values
mapped.sort(function (a, b) {
    return a.day - b.day || a.hour.localeCompare(b.hour);
});

// container for the resulting order
var result = mapped.map(function (el) {
    return data[el.index];
});

document.write('<pre>' + JSON.stringify(result, 0, 4) + '</pre>');
Run code
+1
source

Source: https://habr.com/ru/post/1629132/


All Articles