Can somebody explain me how to use Comparator class for sorting using Arrays.sort?

I have a 2d array.. supoose i want to sort the array 4 by 4 (let us say) using column 1 of array(let us say)...

Edited by deceptikon: Moved to Java forum

4 Years
Discussion Span
Last Post by JamesCherrill

First thing to remember is that Java doesn't really have a "2D array". All arrays have a single dimension BUT any element may itself be an array (etc recursively). So what you have is an array of arrays. It sounds like you want to sort the "outer" array using the first element of each sub-array for the sort order.

You can create a comparator that compares two Objects, and in it you cast the Objects to Object[] and compare their first elements. You can then use that comparator to sort an array of arrays.

ps Is your "read CSV file" question solved?

Edited by JamesCherrill


Yeah, i could get throught the read CSV file problem... I have marked it solved...
Could you please give me an example of a code that use comparator class... ? I could better understand by that way... `

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.