# Sort an array with user defined datatype in java

This question is from geeksforgeeks :

Q) Given two unsorted arrays A of size N and B of size M of distinct elements, the task is to find all pairs from both arrays whose sum is equal to X.

```import java.util.*;
import java.lang.*;
import java.io.*;

class pair  {
long first, second;
public pair(long first, long second)
{
this.first = first;
this.second = second;
}
}

class GFG {
public static void main(String[] args) throws IOException
{
while(t-->0)
{

long N = Long.parseLong(stt.nextToken());
long M = Long.parseLong(stt.nextToken());
long X = Long.parseLong(stt.nextToken());
long A[] = new long[(int)(N)];
long B[] = new long[(int)(M)];

for (int i = 0; i < N; i++) {
A[i] = Long.parseLong(inputLine[i]);
}
for (int i = 0; i < M; i++) {
B[i] = Long.parseLong(inputLine1[i]);
}

Solution obj = new Solution();
pair [] answer = obj.allPairs(A, B, N, M, X);

if(sz==0)
System.out.println(-1);
else{
StringBuilder output = new StringBuilder();
for(int i=0;i<sz;i++){
if(i<sz-1)
else

}
System.out.println(output);
}

}
}
}
// } Driver Code Ends

//User function Template for Java

/*
class pair  {
long first, second;
public pair(long first, long second)
{
this.first = first;
this.second = second;
}
}
*/

class Solution {
public pair[] allPairs( long A[], long B[], long N, long M, long X) {

//MY CODE STARTS FROM HERE
ArrayList<pair> list = new ArrayList<>();
HashSet<Long> set = new HashSet<>();
for(long i : A){
}

for(int i=0;i<M;i++){
if(set.contains(X-B[i])){
}
}

pair arr[] = new pair[list.size()];
arr = list.toArray(arr);
for(int j=0;j<(arr.length-1);j++){
if(arr[j]>arr[j+1]){
pair temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
j -= 1;
}

}
return arr;
}
}
```

I have to sort the result in ascending order but the datatype for array here is user-defined (i.e. pair). So I am getting this error :

prog.java:100: error: bad operand types for binary operator ‘>’ if(arr[j]>arr[j+1]){ ^ first type: pair second type: pair 1 error

Why use the custom sort when you can use built-in sort in Java list.

Just use `list.sort(Comparator.comparing(p -> ((pair) p).first).thenComparing(p -> ((pair) p).second));` to sort the pair list.

```public pair[] allPairs( long A[], long B[], long N, long M, long X) {
//MY CODE STARTS FROM HERE
ArrayList<pair> list = new ArrayList<>();
HashSet<Long> set = new HashSet<>();
for(long i : A){
}

for(int i=0;i<M;i++){
if(set.contains(X-B[i])){