How to sort an array sequencer in javascript?

I’m not sure if the title of this question is correct or not, and also don’t know which suitable keyword to search in google.

My array looks like this:

var myArray = [1,1,2,2,2,3,4,4,4];

and I want to sort the array into:

var myArray = [1,2,3,4,1,2,4,2,4];

Please, to my expected result. the order increases, but the repeated value is repeated in the last sequence, and does not stack together in adjacent keys. Thus, the expected result is grouped as 1,2,3,4 1,2,4and 2,4.

Thanks for your help and sorry for my bad english.

+4
source share
6 answers

This code works. But this may be the best solution.

// We assume myArray is already sorted
var myArray = [1,1,2,2,2,3,4,4,4],
    result = [];

while (myArray.length) {
   var value = myArray.shift();

   // Find place
   var index = 0;
   while(result[index] && result[index][result[index].length - 1] == value) index++;

   if(!result[index]) {
      result[index] = [];
   }  

   result[index][result[index].length] = value;
}

result.reduce(function(current, sum) {
  return current.concat(sum);
});

console.log(result) // Display [1, 2, 3, 4, 1, 2, 4, 2, 4]
+2
source

JQuery, , .

tempResultArray, , .

, .

var myArray = [1,1,2,2,2,3,4,4,4],result = [];

while (myArray && myArray.length) {
    myArray = customSort(myArray);
}
console.log(result);

function customSort(myArray){
    var tempResultArray = [], tempMyArray = [];
    $.each(myArray, function(i, el){
        if($.inArray(el, tempResultArray ) === -1){ 
            tempResultArray.push(el);
        }else{
            tempMyArray.push(el); 
        }
   });  
    tempResultArray.sort(function(a, b){return a-b});
    $.merge( result,tempResultArray)
    return tempMyArray;
}

JSFiddle

+2

.

function sprout(array) {
    return array.reduce(function (r, a) {
        !r.some(function (b) {
            if (b[b.length - 1] < a) {
                b.push(a);
                return true;
            }
        }) && r.push([a]);
        return r;
    }, []).reduce(function (r, a) {
        return r.concat(a);
    });
}

document.write('<pre>' + JSON.stringify(sprout([1, 1, 2, 2, 2, 3, 4, 4, 4]), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(sprout([1, 2, 3, 7, 7, 7]), 0, 4) + '</pre>');
document.write('<pre>' + JSON.stringify(sprout([1, 1, 2, 3, 3, 4, 5, 5, 6, 6, 6, 6, 6, 7]), 0, 4) + '</pre>');
Hide result
+2

:

var myArray = [1, 1, 2, 2, 2, 3, 4, 4, 4];


function mySequelArray(arr) {
    var res = arguments[1] || [];
    var nextVal;
    var min = Math.min.apply(null, arr);

    if (res.length > 0) {
        nextVal = arr.filter(function (x) {
            return x > res[res.length - 1]
        }).sort()[0] || min;
    } else {
        nextVal = min;
    }
    res.push(nextVal);
    arr.splice(arr.indexOf(nextVal), 1);

    return (arr.length > 0) ? mySequelArray(arr, res) : res;


}

console.log(mySequelArray(myArray))
Hide result

fiddle

+2

, , .

+1

: , , inputArray . , , . , , . , inputArray - . Jsfiddle .

var inputArray = [3, 4, 1, 2, 3, 1, 2, 4, 1, 2, 5, 1];
var result = sortInSequence(inputArray);
console.log(result); //output: [1, 2, 3, 4, 5, 1, 2, 3, 4, 1, 2, 1]

function sortInSequence(inputArray){
    var inputArraySize = inputArray.length,
        tempArray = [], //holds new array we are populating
        sameValuesArray = [], //holds same values that we will pass as param in recursive call
        rSorted = []; //init sorted array in case we have no same values

    for(var i = inputArraySize; i > 0; i--){
        var value = inputArray.pop();
        tempArray.push(value);

        var counter = 0,
            tempArraySize = tempArray.length;
        for(var j = 0; j < tempArraySize; j++){
            if(tempArray[j] == value){
                counter++;
            }
        }

        if(counter == 2){
            //value found twice, so remove it from tempArray and add it in sameValuesArray
            var sameValue = tempArray.pop();
            sameValuesArray.push(sameValue);
        }
    }

    if(sameValuesArray.length > 0){
        rSorted = sortInSequence(sameValuesArray);
    }

    tempArray.sort();
    return tempArray.concat(rSorted);
}
+1

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


All Articles