0

I have two arrays and I want the first one to be sorted by the value in my second array. My problem is that the elements of my first array is a string, my second array is only integers. The array that I want to achieve must have the lowest time to the highest time

First array:

[
 '<tr data-time="1437513780"><td>test01</td>', 
 '<tr data-time="1435779420"><td>test02</td>', 
 '<tr data-time="1438316400"><td>test03</td>'
]

Second array:

[
 1435779420, 
 1437513780, 
 1438316400
]

The array I want to achieve :

[
 '<tr data-time="1435779420"><td>test02</td>', 
 '<tr data-time="1437513780"><td>test01</td>', 
 '<tr data-time="1438316400"><td>test03</td>'
]

EDIT: What I need is a new array based on the values of the second array but the same way it looks in the first array!

Matthieu Boisjoli
  • 1,057
  • 2
  • 11
  • 17

4 Answers4

2

If I understand you correctly, you don't have to sort based on the second array. Just extract the numbers and sort by them.

var r = /\d+/;
var arrToSort = ['<tr data-time="1437513780"><td>test01</td>', '<tr data-time="1435779420"><td>test02</td>', '<tr data-time="1438316400"><td>test03</td>'];

sortedArr = arrToSort.sort(function(a, b) { return a.match(r) - b.match(r); })
durrrutti
  • 1,020
  • 1
  • 8
  • 18
  • Actually, I do need to sort my first array depending on the values of the second array...sorry if I didnt expressed myself correctly. – Matthieu Boisjoli Jul 06 '15 at 17:15
  • "The array that I want to achieve must have the lowest time to the highest time." That's what the above answer accomplishes. – durrrutti Jul 06 '15 at 17:17
  • Hello durrrutti, this is exactly what I was looking for. Thank you very much for your time, I will accept your answer and see if I can understand what you did ;) – Matthieu Boisjoli Jul 06 '15 at 17:24
1

Seems like this:

var a = ['<tr data-time="1437513780"><td>test01</td>', '<tr data-time="1435779420"><td>test02</td>', '<tr data-time="1438316400"><td>test03</td>'];
var b = [1435779420, 1437513780, 1438316400];

var res = a.map(function(x, i) { return { val:x, key:b[i] } })
           .sort(function(x, y) { return (x.key>y.key) - (x.key<y.key) })
           .map(function(x) { return x.val });

But there is different order of data-time in first array and values in the second one in the question.


If you need to reorder the first array according to the second:

var a = ['<tr data-time="1437513780"><td>test01</td>', '<tr data-time="1435779420"><td>test02</td>', '<tr data-time="1438316400"><td>test03</td>'];
var b = [1435779420, 1437513780, 1438316400];

var temp = {};

for (var q=0; q<a.length; ++q) {
  var match = a[q].match(/\bdata-time=(["']?)(\d+)\1/);
  temp[match ? match[2] : ""] = a[q];
}

var res = b.map(function(x) { return temp[x] });

// Test: compare res with version from the question:
res == "" + ['<tr data-time="1435779420"><td>test02</td>', '<tr data-time="1437513780"><td>test01</td>', '<tr data-time="1438316400"><td>test03</td>']
Qwertiy
  • 19,681
  • 15
  • 61
  • 128
  • Hello Qwertiy, yes there is a different order of data-time in the first array and the second one. I need to "resort" the first one depending on the values of the second array. – Matthieu Boisjoli Jul 06 '15 at 17:14
  • @MatthieuBoisjoli, then I don't understand what do you meen by sorting. Or do you meen, you want to reorder first array according to the order of second one? – Qwertiy Jul 06 '15 at 17:29
1

since the natural Array-sorting of JavaScript is from low to high, You can use the default sort function for the Array, like this:

['<tr data-time="1437513780"><td>test01</td>', '<tr data-time="1435779420"><td>test02</td>', '<tr data-time="1438316400"><td>test03</td>'].sort()

but, keep in mind this will work only if you use the same template for all elements in your array, AND the date-time numbers are always 10 digits long. otherwise, you need to use a custom sort function.

Shimon Brandsdorfer
  • 1,673
  • 9
  • 23
0

You don't actually need the 2nd array. You have the values you want to sort by in the string of the first array. Instead, you can take advantage of the fact that javascript's inbuilt Array.sort function takes in a function, compare. You can use this to customize the sorting behavior.

The function takes in two arguments, the two values being sorted. If the first value is "greater than" the second value, the function should return a positive number. If the first value is "less than" the second value, the function should return a negative number. If they are equal, return 0.

To do this:

array.sort(function(element1, element2){
    var num1 = parseInt(element1.match(/data-time="(\d+)"/)[1]);
    var num2 = parseInt(element2.match(/data-time="(\d+)"/)[1]);
    if(num1 > num2)
        return 1;
    if(num1 < num2)
        return -1;
    return 0;
});

The regex matches data-time="(numbers)" and index1 matches the numbers in the regex match group. The parseInt function will turn those into numbers. Then, we can use these numbers to create normal sorting behavior.

EDIT: If you need it to sort based on the order already in the firstarray, a slight modification is necessary. You need to find the index of the numbers in the 2nd array.

array.sort(function(element1, element2){
    var num1 = parseInt(element1.match(/data-time="(\d+)"/)[1]);
    var num2 = parseInt(element2.match(/data-time="(\d+)"/)[1]);
    num1 = array2.indexOf(num1);
    num2 = array2.indexOf(num2);
    if(num1 > num2)
        return 1;
    if(num1 < num2)
        return -1;
    return 0;
});
bchoi
  • 126
  • 5