简体   繁体   中英

Sort array using the positions

I've got an array as below.

var FruitArr = [5, "Mango", 3, "Apple", 2, "Lychee", 1, "Banana", 4, "Pineapple"];

How can I sort the fruit names according to the number before it and add to an empty array? The array has been stored as position , item .

The expected output is

var newFruitArr = ["Banana", "Lychee", "Apple", "Pineapple", "Mango"];

EDIT:

The reason for having items as it is shown: In my actual code the fruit names are base64 url string which is created on the fly. The base64 creating depends based on the image. Therefore I couldn't think of a better way of adding the url strings in to the array. So I added items to the array as 'desired position', 'base64 string'. I thought of sorting them once all conversions are done. I did use .splice() which did not work as expected because of the above reason.

Does this fit your need ?

function sort (arr) {
    var min, minId = -1, output = [];

    while (arr.length >= 2) {
        for (var i = 0; i < arr.length; i += 2) {
            if (arr[i] < min || minId == -1) {
                minId = i;
                min = arr[i];
            }
        }
        output.push(arr[minId + 1]);
        arr.splice(minId, 2);

        minId = -1;
    }

    return output;
}

It search for the minimum number, push the corresponding fruit to the output and remove the couple from the input array, until there's nothing in it. Quite simple, surely not the most effective solution.

There is no need to sort, you already have the indexes in your input array.

Just preallocate your new array and fill it.

var fruits = [2, "apple", 1, "orange"],
    fruitsLength = fruits.length;

var newFruitArr = new Array(fruitsLength / 2);
for (var i = 0; i < fruitsLength; i += 2)
    newFruitArr[fruits[i] - 1] = fruits[i + 1];

You have to convert your array to a form easy to use with sort method.

Here is the code to do so:

var result = [];
FruitArr.forEach(function (el, i) {
    if (i % 2) result.push({value: el, weight: FruitArr[i-1]});
});

The result array will be:

[{value: "Mango", weight: 5}, {value: "Apple", weight: 3}, {value: "Lychee", weight: 2}, {value: "Bananna", weight: 1}, {value: "Pineapple", weight: 4}];

which easy to sort with sort method.

I actually prefer insertion-sort-algo to sort an array because of performance issues:

var arr = [5, "Mango", 3, "Apple", 2, "Lychee", 1, "Bananna", 4, "Pineapple"];
var groups = [];

for(var f=0; f < arr.length; f+=2)groups.push([arr[f],arr[f+1]]);

function insertion_sort(array){
  for(var o=1; o < array.length;o++){
    for(var i=o; i>0 && array[i][0] < array[i-1][0];i--){
       var tmp = array[i];
       array[i] = array[i-1];
       array[i-1] = tmp;
    }
  }
  return array;
}

insertion_sort(groups); // [[1, "Bananna"], [2, "Lychee"], [3, "Apple"], [4, "Pineapple"], [5, "Mango"]]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM