First, do a quick sort or merge sort on the array. Then set the two pointers to the first and the last element. Check the numbers in-between and calculate new sums and delta by binary search. After that move the two pointers according to the value of della.

The sort takes (O(nlgn)), the outer loop for two pointers takes O(n) and testing numbers in-between takes O(lgn), so total should be O(nlgn).

Following is my code:

```
public int threeSumClosest(int[] num, int target) {
if (num == null || num.length <=2) return Integer.MIN_VALUE;
int i = 0, j = num.length - 1, delta = Integer.MAX_VALUE;
Arrays.sort(num);
while(i+1<j){
delta = checkSum(num, i, j, target, delta);
if (delta > 0)
--j;
else if (delta < 0)
++i;
else
break;
}
return target + delta;
}
public int checkSum(int[] num, int left, int right, int target, int delta){
int lnum = num[left], rnum = num[right];
++left; --right;
int mid;
while(left <= right){
mid = (left+right)/2;
int sum = lnum + rnum + num[mid];
if (Math.abs(sum-target) < Math.abs(delta))
delta = sum - target;
if (sum > target)
right = mid - 1;
else
left = mid + 1;
}
return delta;
}
```