How to Sort Elements in an Array into Order and Even Numbers and They Should Be in Ascending Order

How to sort even numbers ascending and odd numbers descending in an array?

A trivial and very expensive solution would be to build two arrays, sort them then concatenate them, but you can combine both tests and sort in place like this:

arr.sort((a,b)=> (a%2-b%2) || (a%2 ? b-a : a-b))

As you can see, the pattern for hierarchical sort is just

arr.sort(compareA || compareB) 

which you can generalize for more conditions.

let arr = Array.from({length:10}, (_,i)=>i+1)arr.sort((a,b)=> (a%2-b%2) || (a%2 ? b-a : a-b))console.log(arr)

How to sort an array of odd numbers in ascending order, but keep even numbers at their position?

You could take a helper array for the odd indices and another for the odd numbers, sort them and apply them back on the previously stored indices of the original array.

var array = [5, 3, 2, 8, 1, 4],    indices = [];
array .filter((v, i) => v % 2 && indices.push(i)) .sort((a, b) => a - b) .forEach((v, i) => array[indices[i]] = v);
console.log(array);

Sort Array in c#, even numbers in descending and odd numbers in ascending without using predefined sort function

The matter is what comparison to perform. Any sorting algorithm is suitable, as long as it uses the correct comparison criteria.

Current comparison criteria are:

  1. Even numbers before odd numbers
  2. Greater even numbers before smaller ones
  3. Smaller odd numbers before greater ones

Choose your favorite sorting algorithm and invoke a comparer that apply those criteria.

Main comparison, just a Comparer for ease of use, but you can extract the Compare method, make it static, and use it alone:

public class EvenOddComparer : Comparer<int>
{
public override int Compare(int value1, int value2)
{
// returns -1 if value1 must be placed before value2
// returns +1 if value1 must be placed after value2
// returns 0 if value1 equals value2

var even1 = value1 % 2 == 0;
var even2 = value2 % 2 == 0;
// first is even and second is odd
if (even1 && !even2)
return -1;
// first is odd and second is even
if (!even1 && even2)
return +1;

// both are even
if (even1)
return -value1.CompareTo(value2); // descending
// both are odd
else
return value1.CompareTo(value2); // ascending
}
}

Sorting method, that receives the array of values and a reference to the comparer. You can remove the comparer parameter and invoke the comparison method of your choice directly instead of comparer.Compare. Here we have a Bubble sort, just for simplicity:

public static void Sort(int[] values, Comparer<int> comparer)
{
for (int ixItem1 = 0; ixItem1 < values.Length - 1; ixItem1++)
for (int ixItem2 = ixItem1 + 1; ixItem2 < values.Length; ixItem2++)
{
var item1 = values[ixItem1];
var item2 = values[ixItem2];

var needSwap = comparer.Compare(item1, item2) > 0;
if (needSwap)
{
values[ixItem1] = item2;
values[ixItem2] = item1;
}
}
}

Sample usage:

var values = new[] { 8, 3, 1, 5, 2, 6, 7, 4 };
Sort(values, new EvenOddComparer());

Sort the numbers so that the even numbers were ahead

You can use the .sort() method and a comparator. The comparator function will have to first check the "evenness" of the two arguments. If they're both even or both odd, it'll then base the comparison result on the value:

array.sort(function(a, b) {
let aeven = !(a % 2), beven = !(b % 2);
if (aeven && !beven) return -1;
if (beven && !aeven) return 1;
return a - b;
});

A comparator function for the .sort() method is passed two values from the array. The function should return:

  • a negative number if the first should sort before the second;
  • a positive number if the first should sort after the second;
  • zero if they're the same for ordering purposes.

Sorting an array in a specific way in the c language

One approach is using a comparision function that says

  • Odd numbers are smaller than even numbers
  • If odd/evenness is same, compare the numbers

Also

  • Your code to swap elements is wrong. p should be assigned instead of arr[i] in the third step.
  • Your loops to search the minimum element are wrong because they are omitting the last element from the searching.
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

int cmp(int a, int b)
{
if (a % 2 != 0 && b % 2 == 0) return -1;
if (a % 2 == 0 && b % 2 != 0) return 1;
if (a < b) return -1;
if (a > b) return 1;
return 0;
}

int main()
{
int arr[26],i,min,j,p;
srand((unsigned)time(NULL));
for(i=0;i<26;i++){
arr[i] = rand() % 100;
}
for(i=0;i<26;i++){
printf("%2d ", i);
}
puts("");
for(i=0;i<26;i++){
printf("%2d ", arr[i]);
}

for(i=0;i<26;i++){
min = i;
for(j = i+1;j<26;j++){
if(cmp(arr[j],arr[min])<0){
min = j;
}
}
p = arr[i];
arr[i] = arr[min];
arr[min] = p;
}

puts("");
for(i=0;i<26;i++){
printf("%2d ", arr[i]);
}

return 0;
}

How do you only order odd numbers in an int array and leave even numbers in their original position?

I used an inner loop to find the next odd number to sort. And I used an additional outer loop because normally sorting needs two loops.

import java.util.Arrays;

public class Kata {
public static int[] sortArray(int[] array) {
int i;
int n;
int m;
int temp = 0;
if (array.length < 2) {
System.out.println(Arrays.toString(array));
return array;
}

for (i = 0; i < array.length; i++) {
for (n = 0; n < array.length; n++) {
if (array[n] % 2 != 0) {
for (m = n + 1; m < array.length; m++) {
if (array[m] % 2 != 0) {
if (array[n] > array[m]) {
temp = array[n];
array[n] = array[m];
array[m] = temp;
}
break;
}
}
}
}
}
System.out.println(Arrays.toString(array));
return array;
}
}

How to sort an array of integers correctly

By default, the sort method sorts elements alphabetically. To sort numerically just add a new method which handles numeric sorts (sortNumber, shown below) -