Sort an integer array keeping first

How can I sort the arrays like this:

[10, 7, 12, 3, 5, 6] --> [10, 12, 3, 5, 6, 7]

[12, 8, 5, 9, 6, 10] --> [12, 5, 6, 8, 9, 10] 

      

  • storing array [0] in place
  • with the next highest integer next (if any)
  • then ascending from the lowest integer
+3


source to share


6 answers


You can store the value of the first item and use it in the state for the first delta sort. Then sort by standard delta.

How it works (sort order from Edge)

              condition  numerical     sortFn
   a      b       delta      delta     result  comment
-----  -----  ---------  ---------  ---------  -----------------
   7     10*          1                     1  different section
  12*     7          -1                    -1  different section
  12*    10*          0          2          2  same section
  12*     7          -1                    -1  same section
   3      7           0         -4         -4  same section
   3     12*          1                     1  different section
   3      7           0         -4         -4  same section
   5      7           0         -2         -2  same section
   5     12*          1                     1  different section
   5      3           0          2          2  same section
   5      7           0         -2         -2  same section
   6      7           0         -1         -1  same section
   6      3           0          3          3  same section
   6      5           0          1          1  same section
   6      7           0         -1         -1  same section

* denotes elements who should be in the first section

      

Elements of different sections mean that one of the elements goes to the first and the second to the second section, the value is taken by the delta condition.

Elements of the same section mean that both elements belong to the same section. Delta values ​​are returned for sorting.



function sort(array) {
    var first = array[0];
    array.sort(function (a, b) {
       return (a < first) - (b < first) || a - b;
    });
    return array;
}

console.log(sort([10, 7, 12, 3, 5, 6]));
console.log(sort([12, 8, 5, 9, 6, 10]));
      

.as-console-wrapper { max-height: 100% !important; top: 0; }
      

Run codeHide result


+8


source


An easy solution might be to sort the entire array and then split the resulting array by the original first element.

those.

  • first sort [10, 7, 12, 3]

    before[3, 7, 10, 12]

  • then split it by >= 10

    and < 10

    : [10, 12]

    and[3, 7]

  • and finally combine both with [10, 12, 3, 7]



An example of implementation without polishing:

function customSort(input) {
  var firstElem = input[0];
  var sortedInput = input.sort(function(a, b) { return a-b; });
  var firstElemPos = sortedInput.indexOf(firstElem);
  var greaterEqualsFirstElem = sortedInput.splice(firstElemPos);
  var lessThanFirstElem = sortedInput.splice(0, firstElemPos);
  return greaterEqualsFirstElem.concat(lessThanFirstElem);
}

console.log(customSort([10, 7, 12, 3, 5, 6]));
console.log(customSort([12, 8, 5, 9, 6, 10]));
console.log(customSort([12, 8, 5, 9, 12, 6, 10]));
console.log(customSort([12]));
console.log(customSort([]));
      

Run codeHide result


+3


source


Check out below snippet.

It's really simple. Just remove the first item from arrary into a new array.

Detach the rest of the array and check if the max of this array is greater than the first element.

If so, paste it into the result. Otherwise concat rest array with result array

var input1 = [10, 7, 12, 3, 5, 6];
var expected1 = [10, 12, 3, 5, 6, 7];

var input2 = [12, 8, 5, 9, 6, 10];
var expected2 = [12, 5, 6, 8, 9, 10];

function customSort(aInput) {
  var aResult = [aInput.shift()];
  aInput = aInput.sort(function(a, b) { return a - b;});
  if (aInput[aInput.length - 1] > aResult[0]) {
    var iMax = aInput.pop();
    aResult.push(iMax);
  }
  aResult = aResult.concat(aInput);
  return aResult;
}

console.log("Expected: ", expected1.toString());
console.log("Sorted: ", customSort(input1).toString());
console.log("Expected: ", expected2.toString());
console.log("Sorted: ", customSort(input2).toString());
      

Run codeHide result


+1


source


Updated **

it doesn't work with array.concat, but with this merge function, no idea why ....

Here's a kind of solution,

Array.prototype.merge = function(/* variable number of arrays */){
    for(var i = 0; i < arguments.length; i++){
        var array = arguments[i];
        for(var j = 0; j < array.length; j++){
            if(this.indexOf(array[j]) === -1) {
                this.push(array[j]);
            }
        }
    }
    return this;
};

var $a = [10, 7, 12, 3, 5, 6];
var $b = [12, 8, 5, 9, 6, 10];

function reorganize($array){

  var $reorganize = [];

  $reorganize.push($array.shift());

  $array.sort(function(a, b) { return a - b; });

  $reorganize.merge($array);

  return $reorganize;
}

console.log(reorganize($a));
console.log(reorganize($b));

      

0


source


My suggestion is based on:

  • reduce the original array to two, containing the bottom and top numbers, down to the first.
  • sort each array and concatenate

Thus, the original task is divided into two sub-processes that are easier to work with.

function cSort(arr) {
    var retval = arr.reduce(function (acc, val) {
        acc[(val < arr[0]) ? 1 : 0].push(val);
        return acc;
    }, [[], []]);
    return retval[0].sort((a, b) => a-b).concat(retval[1].sort((a, b) => a-b));
}

//
//  test
//
console.log(cSort([10, 7, 12, 3, 5, 6]));
console.log(cSort([12, 8, 5, 9, 6, 10]));
console.log(cSort([12, 8, 5, 9, 12, 6, 10]));
console.log(cSort([12]));
console.log(cSort([]));
      

Run codeHide result


0


source


I am using Java:

     int[] arr = {10, 7, 12, 3, 5, 6};
     List<Integer> arr_1 = new ArrayList<Integer>();
     for(int i = 0; i < arr.length; i++)
     {
         arr_1.add(arr[i]);
     }
     Collections.sort(arr_1.subList(1,arr_1.size()));


    System.out.println(arr_1);

      

0


source







All Articles