1

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.

Becky
  • 5,467
  • 9
  • 40
  • 73
  • 1
    Why are you using an array for this kind of data instead of a json object? If you change your FruitArr to `{"5": "Mango", "3": "Apple", "2": "Lychee", "1": "Bananna", "4": "Pineapple"};` it might work like in this [answer](http://stackoverflow.com/questions/5467129/sort-javascript-object-by-key) – Sebastian Aug 11 '15 at 10:21
  • That's a pretty crazy way to store that array, I'd expect something more like `[{position: 5, item: "Mango"},{position: 2, item: "Apple}...etc]` – Starscream1984 Aug 11 '15 at 10:22

4 Answers4

2

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.

2

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];
Eloims
  • 5,106
  • 4
  • 25
  • 41
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.

hindmost
  • 7,125
  • 3
  • 27
  • 39
1

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"]]
Blauharley
  • 4,186
  • 6
  • 28
  • 47