<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Sorting a 2-Dimensional Array in Java</title>
	<atom:link href="http://publicmind.in/blog/sorting-2d-array-java/feed/" rel="self" type="application/rss+xml" />
	<link>http://publicmind.in/blog/sorting-2d-array-java/</link>
	<description>Simple and Sophisticated</description>
	<lastBuildDate>Thu, 29 Jul 2010 08:08:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
	<item>
		<title>By: agil</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/comment-page-1/#comment-1074</link>
		<dc:creator>agil</dc:creator>
		<pubDate>Sun, 13 Jun 2010 17:40:40 +0000</pubDate>
		<guid isPermaLink="false">http://publicmind.in/blog/?p=199#comment-1074</guid>
		<description>Hello Nitin,

Your solution very useful for two Dimensional array with same length.
My question is, can we sort two dimensional array based on some row and based on data length. for example we have an array {{”c”}, {”c”}, {”c”, “d”},{”c”, “a”},{”b”}}. how to get the sorted array like this {{”b”},{”c”},{”c”},{”c”,”a”},{”c”, “d”}}.

Thx a lot Nitin.</description>
		<content:encoded><![CDATA[<p>Hello Nitin,</p>
<p>Your solution very useful for two Dimensional array with same length.<br />
My question is, can we sort two dimensional array based on some row and based on data length. for example we have an array {{”c”}, {”c”}, {”c”, “d”},{”c”, “a”},{”b”}}. how to get the sorted array like this {{”b”},{”c”},{”c”},{”c”,”a”},{”c”, “d”}}.</p>
<p>Thx a lot Nitin.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Bizunas</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/comment-page-1/#comment-1047</link>
		<dc:creator>Bizunas</dc:creator>
		<pubDate>Sat, 29 May 2010 14:20:59 +0000</pubDate>
		<guid isPermaLink="false">http://publicmind.in/blog/?p=199#comment-1047</guid>
		<description>Thanks, it helped me alot. No changes needed at all :)</description>
		<content:encoded><![CDATA[<p>Thanks, it helped me alot. No changes needed at all <img src='http://publicmind.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: autofei</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/comment-page-1/#comment-1040</link>
		<dc:creator>autofei</dc:creator>
		<pubDate>Fri, 28 May 2010 16:24:01 +0000</pubDate>
		<guid isPermaLink="false">http://publicmind.in/blog/?p=199#comment-1040</guid>
		<description>I just post a working version modified from Lars version. Hope it helpful.</description>
		<content:encoded><![CDATA[<p>I just post a working version modified from Lars version. Hope it helpful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: autofei</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/comment-page-1/#comment-1039</link>
		<dc:creator>autofei</dc:creator>
		<pubDate>Fri, 28 May 2010 16:23:00 +0000</pubDate>
		<guid isPermaLink="false">http://publicmind.in/blog/?p=199#comment-1039</guid>
		<description>import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Sorter {

	@SuppressWarnings(&quot;unchecked&quot;)
	public static  void sort(final T[][] toSort,
			final int onColumn) {
		List list = Arrays.asList(toSort);
		Collections.sort(list, new Comparator() {
			public int compare(T[] a, T[] b) {
				return a[onColumn].compareTo(b[onColumn]);
			}

			@Override
			public int compare(Object o1, Object o2) {
				T[] a = (T[])o1;
				T[] b = (T[])o2;
				return compare(a,b);
			}
		});
	}

	public static void main(String[] args) {
		final String[][] array = new String[][] { { &quot;foo&quot;, &quot;bar&quot; },
				{ &quot;a&quot;, &quot;b&quot; }, { &quot;me&quot;, &quot;you&quot; }, { &quot;by&quot;, &quot;now&quot; } };

		System.out.println(Arrays.deepToString(array));
		Sorter.sort(array, 0);
		System.out.println(Arrays.deepToString(array));
	}
}</description>
		<content:encoded><![CDATA[<p>import java.util.Arrays;<br />
import java.util.Collections;<br />
import java.util.Comparator;<br />
import java.util.List;</p>
<p>public class Sorter {</p>
<p>	@SuppressWarnings(&#8220;unchecked&#8221;)<br />
	public static  void sort(final T[][] toSort,<br />
			final int onColumn) {<br />
		List list = Arrays.asList(toSort);<br />
		Collections.sort(list, new Comparator() {<br />
			public int compare(T[] a, T[] b) {<br />
				return a[onColumn].compareTo(b[onColumn]);<br />
			}</p>
<p>			@Override<br />
			public int compare(Object o1, Object o2) {<br />
				T[] a = (T[])o1;<br />
				T[] b = (T[])o2;<br />
				return compare(a,b);<br />
			}<br />
		});<br />
	}</p>
<p>	public static void main(String[] args) {<br />
		final String[][] array = new String[][] { { &#8220;foo&#8221;, &#8220;bar&#8221; },<br />
				{ &#8220;a&#8221;, &#8220;b&#8221; }, { &#8220;me&#8221;, &#8220;you&#8221; }, { &#8220;by&#8221;, &#8220;now&#8221; } };</p>
<p>		System.out.println(Arrays.deepToString(array));<br />
		Sorter.sort(array, 0);<br />
		System.out.println(Arrays.deepToString(array));<br />
	}<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nitin</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/comment-page-1/#comment-655</link>
		<dc:creator>Nitin</dc:creator>
		<pubDate>Wed, 24 Feb 2010 07:02:16 +0000</pubDate>
		<guid isPermaLink="false">http://publicmind.in/blog/?p=199#comment-655</guid>
		<description>Lars,

I just formatted your comment to make it clear for the readers, I hope you don&#039;t mind. :)

Regards,</description>
		<content:encoded><![CDATA[<p>Lars,</p>
<p>I just formatted your comment to make it clear for the readers, I hope you don&#8217;t mind. <img src='http://publicmind.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Regards,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lars</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/comment-page-1/#comment-640</link>
		<dc:creator>Lars</dc:creator>
		<pubDate>Tue, 23 Feb 2010 12:59:42 +0000</pubDate>
		<guid isPermaLink="false">http://publicmind.in/blog/?p=199#comment-640</guid>
		<description>Ok, now i see what went wrong. Of course and as usual all generics related &lt;T&gt; are eaten up by the browser. Actually my fault not to write them correctly. The correct version copied from my compiling IDE looks like


package example.sort2d;

import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Sorter {

    public static &lt;T extends Comparable&lt;T&gt;&gt; void sort(final T [][] toSort,final int onColumn) {
        List&lt;T[]&gt; list = Arrays.asList(toSort);
        Collections.sort(list, new Comparator&lt;T[]&gt;(){
            public int compare(T[] a, T[] b) {
                return a[onColumn].compareTo(b[onColumn]);
            }
        });
    }
}

This blog might use some preview, that would be helpful.</description>
		<content:encoded><![CDATA[<p>Ok, now i see what went wrong. Of course and as usual all generics related &lt;T&gt; are eaten up by the browser. Actually my fault not to write them correctly. The correct version copied from my compiling IDE looks like</p>
<p>package example.sort2d;</p>
<p>import java.util.Arrays;<br />
import java.util.Collections;<br />
import java.util.Comparator;<br />
import java.util.List;</p>
<p>public class Sorter {</p>
<p>    public static &lt;T extends Comparable&lt;T&gt;&gt; void sort(final T [][] toSort,final int onColumn) {<br />
        List&lt;T[]&gt; list = Arrays.asList(toSort);<br />
        Collections.sort(list, new Comparator&lt;T[]&gt;(){<br />
            public int compare(T[] a, T[] b) {<br />
                return a[onColumn].compareTo(b[onColumn]);<br />
            }<br />
        });<br />
    }<br />
}</p>
<p>This blog might use some preview, that would be helpful.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nitin</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/comment-page-1/#comment-638</link>
		<dc:creator>Nitin</dc:creator>
		<pubDate>Tue, 23 Feb 2010 12:48:59 +0000</pubDate>
		<guid isPermaLink="false">http://publicmind.in/blog/?p=199#comment-638</guid>
		<description>@Anonymous Genius

I will keep that in mind, its just that I transition between different languages daily that sometimes they get mixed up.

Regards,</description>
		<content:encoded><![CDATA[<p>@Anonymous Genius</p>
<p>I will keep that in mind, its just that I transition between different languages daily that sometimes they get mixed up.</p>
<p>Regards,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Nitin</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/comment-page-1/#comment-637</link>
		<dc:creator>Nitin</dc:creator>
		<pubDate>Tue, 23 Feb 2010 12:46:53 +0000</pubDate>
		<guid isPermaLink="false">http://publicmind.in/blog/?p=199#comment-637</guid>
		<description>Lars,

Its no problem, and I think you are missing &lt; T[] &gt; from your comparator declaration, which is causing the compilation error, probably what geek is talking about. :)

It should be:

new Comparator&lt; T[] &gt;() { ...

Regards,</description>
		<content:encoded><![CDATA[<p>Lars,</p>
<p>Its no problem, and I think you are missing < T[] > from your comparator declaration, which is causing the compilation error, probably what geek is talking about. <img src='http://publicmind.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>It should be:</p>
<p>new Comparator< T[] >() { &#8230;</p>
<p>Regards,</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Anonymous Genius</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/comment-page-1/#comment-635</link>
		<dc:creator>Anonymous Genius</dc:creator>
		<pubDate>Tue, 23 Feb 2010 08:50:47 +0000</pubDate>
		<guid isPermaLink="false">http://publicmind.in/blog/?p=199#comment-635</guid>
		<description>@Nitin : Thanks for the post. It was quite helpful. However I&#039;d advice you to stick to Java naming conventions even with small examples like this. :)

@Lars : Thanks for the alternative. It was helpful too! :)

@geek : It&#039;s you who should &quot;do some groundwork before posting!&quot; :P</description>
		<content:encoded><![CDATA[<p>@Nitin : Thanks for the post. It was quite helpful. However I&#8217;d advice you to stick to Java naming conventions even with small examples like this. <img src='http://publicmind.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>@Lars : Thanks for the alternative. It was helpful too! <img src='http://publicmind.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>@geek : It&#8217;s you who should &#8220;do some groundwork before posting!&#8221; <img src='http://publicmind.in/blog/wp-includes/images/smilies/icon_razz.gif' alt=':P' class='wp-smiley' /> </p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Lars</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/comment-page-1/#comment-634</link>
		<dc:creator>Lars</dc:creator>
		<pubDate>Tue, 23 Feb 2010 08:33:04 +0000</pubDate>
		<guid isPermaLink="false">http://publicmind.in/blog/?p=199#comment-634</guid>
		<description>@Nitin

Hi Nitin,

First of all sorry, my comment may have sounded more harsh than it was supposed to be. And you are right, somehow i stopped reading before your &quot;note&quot; (I must admit it was late here).

@geek
Sorry, i don&#039;t understand your point. Please have a look at http://java.sun.com/javase/6/docs/api/java/util/Comparator.html#compare(T, T)

Best regards,
Lars</description>
		<content:encoded><![CDATA[<p>@Nitin</p>
<p>Hi Nitin,</p>
<p>First of all sorry, my comment may have sounded more harsh than it was supposed to be. And you are right, somehow i stopped reading before your &#8220;note&#8221; (I must admit it was late here).</p>
<p>@geek<br />
Sorry, i don&#8217;t understand your point. Please have a look at <a href="http://java.sun.com/javase/6/docs/api/java/util/Comparator.html#compare(T" rel="nofollow">http://java.sun.com/javase/6/docs/api/java/util/Comparator.html#compare(T</a>, T)</p>
<p>Best regards,<br />
Lars</p>
]]></content:encoded>
	</item>
</channel>
</rss>
