You should merge the arrays first and then sort the array.
You can easily merge the arrays by creating an ArrayList and for each element in your arrays, add them to the ArrayList, like this :
// just an example of values
int[] lArr = {1,2,8};
// just an example
int[] rArr = {-7,54,9,34,27};
ArrayList<Integer> mergedList = new ArrayList<Integer>();
// Loop through lArr and add each element to mergedList
for(int element : lArr) mergedList.add(element);
// Loop through rArr and add each element to mergedList
for(int element : rArr) mergedList.add(element);
Now you can sort the list using Collections.sort(list, comparator)
, like this :
Collections.sort(mergedList, new Comparator<Integer>() {
// A comparator compares two value (logic)
// This function needs to return 1 if a > b and -1 if b < a
// and if a = b then 1 or -1 won't change anything
public int compare(Integer a, Integer b){
if(a > b) return 1;
return -1;
}
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…