1. 

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS11
* Problem: 11321 Sort! Sort!! and Sort!!!
* Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2296
*
* @author Rolf Schirm
* @version 1.0, 04/26/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 2.424
*/
public class Main {

public static void main(String... args) throws Exception {
final BufferedReader scan = new BufferedReader(new
InputStreamReader(System.in));

// Create array for the numbers
final int[] array = new int[10000];

// Map for the output
final Map<Integer, List<Integer>> map = new TreeMap<Integer,
List<Integer>>();

String line;
while((line = scan.readLine()) != null) {
if(!line.isEmpty()) {
String[] split = line.split(" ");
// The amount of numbers in this test case
int count = Integer.parseInt(split[0]);

// The modulo number
int mod = Integer.parseInt(split[1]);

// Print headline
System.out.println(line);

// Termination condition
if(count == 0 && mod == 0) {
break;
}

// Read all numbers
for(int i = 0; i < count; i++) {
String str = scan.readLine();
if(!str.isEmpty()) {
array[i] = Integer.parseInt(str);
} else {
i--;
}
}

// Sort the numbers
Arrays.sort(array, 0, count);

// Clear the map for this test case
map.clear();

for(int i = 0; i < count; i++) {
// Current number
int number = array[i];

// Mod value from the current number
int modValue = number % mod;

// Check if the list exists
if(!map.containsKey(modValue)) {
// Create a new list for the mod value
map.put(modValue, new ArrayList<Integer>());
}

// Select the list with current modValue
List<Integer> list = map.get(modValue);

// Even number
if((number&1) == 0) {
// Add the number at the end
list.add(number);
}
// Odd number
else {
// Add the number at the start
list.add(0, number);
}
}

// Print the numbers
for(Integer key: map.keySet()) {
for(Integer number: map.get(key)) {
System.out.println(number);
}
}
}
}
}
}

2.


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Arrays;
import java.util.StringTokenizer;

/**
* FWP, Ausgewählte Probleme aus dem ACM Programming Contest, SS11
* Problem: 11321 Sort! Sort!! and Sort!!!
* Link:
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=25&page=show_problem&problem=2296
*
* @author Bastian Hoecker
* @author Philipp Hauck Thalheim
* @version 1.0, 05/11/2011
*
* Method : Ad-Hoc
* Status : Accepted
* Runtime: 0.100
*/

public class Main {

public static class input implements Comparable<input> {

public int num = 0;
public int mod = 0;
public boolean odd = false;

@Override
public int compareTo(input arg0) {

if (mod < arg0.mod)
return -1;

if (mod == arg0.mod) {
if (num == arg0.num)
return 0;
if (odd && !arg0.odd)
return -1;
if (odd && arg0.odd && num > arg0.num)
return -1;
if (!odd && !arg0.odd && num < arg0.num)
return -1;
}

return 1;
}
}

public static void main(String[] args) throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
System.in));
//Array var der Zahlen
input[] a;

int n;
int m;

while (true) {
StringTokenizer st = new StringTokenizer(reader.readLine());
n = Integer.parseInt(st.nextToken());
m = Integer.parseInt(st.nextToken());
a = new Main.input[n];
if (n == 0 && m == 0) {
System.out.println("0 0");
break;
}

System.out.printf("%d %d\n", n, m);

//Einlesen der Zahlen
for (int i = 0; i < n; i++) {
a[i] = new input();
a[i].num = Integer.parseInt(reader.readLine());
a[i].mod = a[i].num % m;
a[i].odd = Math.abs(a[i].num) % 2 != 0;
}
//Sortieren des Arrays mit Comperator
Arrays.sort(a);
//Ausgabe
for (int i = 0; i < n; i++) {
System.out.println(a[i].num);
}
}
}

}