<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	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/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Public Mindjava</title>
	<atom:link href="http://publicmind.in/blog/tag/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://publicmind.in/blog</link>
	<description>Simple and Sophisticated</description>
	<lastBuildDate>Sat, 04 Sep 2010 06:00:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>MYSQL: Get the Auto-Increment Values after Insert Statement</title>
		<link>http://publicmind.in/blog/mysql-auto-increment-values/</link>
		<comments>http://publicmind.in/blog/mysql-auto-increment-values/#comments</comments>
		<pubDate>Wed, 15 Jul 2009 12:24:32 +0000</pubDate>
		<dc:creator>Nitin</dc:creator>
				<category><![CDATA[Nitin's Blog]]></category>
		<category><![CDATA[Intern]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[php]]></category>

		<guid isPermaLink="false">http://publicmind.in/blog/?p=218</guid>
		<description><![CDATA[Until sometime back, I was unaware of how I could get the value of an auto-incremented field after an Insert Statement. But recently I needed it for two of my projects simultaneously, so I explored the possibilities. As one of my project is in Java and other is in PHP, we will go through both [...]]]></description>
			<content:encoded><![CDATA[<p>Until sometime back, I was unaware of how I could get the value of an auto-incremented field after an Insert Statement. But recently I needed it for two of my projects simultaneously, so I explored the possibilities. As one of my project is in Java and other is in PHP, we will go through both of them. </p>
<p>So, here is the problem. In general, the PRIMARY KEY field is the AUTO_INCREMENT field. Now wen you insert a row, a new key is generated and you can&#8217;t get it through usual way as this row can be accessed only using the primary key.</p>
<p>So here is how it can be done:<br />
<span id="more-218"></span><br />
<strong>In JAVA: </strong></p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #666666; font-style: italic;">/*Insert a row into the desired table that generates <br />
an auto increment field*/</span><br />
<br />
stmt.<span style="color: #006633;">executeUpdate</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Insert into tableName (col1, col2) values (val1, val2)&quot;</span>, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astatement+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Statement</span></a>.<span style="color: #006633;">RETURN_GENERATED_KEYS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aresultset+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">ResultSet</span></a> rs <span style="color: #339933;">=</span> stmt.<span style="color: #006633;">getGeneratedKeys</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000066; font-weight: bold;">int</span> autoIncValue <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000000; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>rs.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <br />
<span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;autoIncValue <span style="color: #339933;">=</span> rs.<span style="color: #006633;">getInt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp;<span style="color: #666666; font-style: italic;">/*You can get more generated keys if they are generated in your code*/</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p><strong>In PHP</strong>:</p>
<p>The mysql_insert_id() function in PHP returns the AUTO_INCREMENT ID generated from the previous INSERT operation.</p>
<div class="codecolorer-container php default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="php codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">&lt;?php</span><br />
<span style="color: #000088;">$con</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_connect"><span style="color: #990000;">mysql_connect</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;hostname&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;username&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;password&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><br />
&nbsp; <span style="color: #009900;">&#123;</span><br />
&nbsp; <a href="http://www.php.net/die"><span style="color: #990000;">die</span></a><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Error while connecting: '</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_error"><span style="color: #990000;">mysql_error</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
<span style="color: #000088;">$db_selected</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_select_db"><span style="color: #990000;">mysql_select_db</span></a><span style="color: #009900;">&#40;</span>database_name<span style="color: #339933;">,</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #000088;">$sql</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;INSERT INTO tablename VALUES (val1,val2)&quot;</span><span style="color: #339933;">;</span><br />
<span style="color: #000088;">$result</span> <span style="color: #339933;">=</span> <a href="http://www.php.net/mysql_query"><span style="color: #990000;">mysql_query</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$sql</span><span style="color: #339933;">,</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;ID of last inserted record is: &quot;</span> <span style="color: #339933;">.</span> <a href="http://www.php.net/mysql_insert_id"><span style="color: #990000;">mysql_insert_id</span></a><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<br />
<a href="http://www.php.net/mysql_close"><span style="color: #990000;">mysql_close</span></a><span style="color: #009900;">&#40;</span><span style="color: #000088;">$con</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #000000; font-weight: bold;">?&gt;</span></div></div>
<p>Note: Be sure to call mysql_insert_id() immediately after a query to get the correct value. The function returns 0 if the previous query does not generate an AUTO_INCREMENT value, or FALSE if no MySQL connection was established. </p>
<p>Enjoy!!</p>
]]></content:encoded>
			<wfw:commentRss>http://publicmind.in/blog/mysql-auto-increment-values/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Sorting a 2-Dimensional Array in Java</title>
		<link>http://publicmind.in/blog/sorting-2d-array-java/</link>
		<comments>http://publicmind.in/blog/sorting-2d-array-java/#comments</comments>
		<pubDate>Sun, 05 Jul 2009 21:47:28 +0000</pubDate>
		<dc:creator>Nitin</dc:creator>
				<category><![CDATA[Nitin's Blog]]></category>
		<category><![CDATA[code]]></category>
		<category><![CDATA[Intern]]></category>
		<category><![CDATA[java]]></category>

		<guid isPermaLink="false">http://publicmind.in/blog/?p=199</guid>
		<description><![CDATA[Recently while working for my project, I came across this situation when I had a 2-D array and I needed to sort it twice on 2 of its columns. Consider the following 2D array: String&#91;&#93;&#91;&#93; testString = new String&#91;&#93;&#91;&#93; &#123; &#160; &#160; &#123;&#34;1&#34;, &#34;2&#34;, &#34;6&#34;&#125;, &#160; &#160; &#123;&#34;4&#34;, &#34;5&#34;, &#34;3&#34;&#125; &#160; &#125;; Sorting the above [...]]]></description>
			<content:encoded><![CDATA[<p><!-- Digg Digg Disabled -->Recently while working for my project, I came across this situation when I had a 2-D array and I needed to sort it twice on 2 of its columns. Consider the following 2D array:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> testString <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;1&quot;</span>, <span style="color: #0000ff;">&quot;2&quot;</span>, <span style="color: #0000ff;">&quot;6&quot;</span><span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;4&quot;</span>, <span style="color: #0000ff;">&quot;5&quot;</span>, <span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Sorting the above 2D array on zero column will give</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;1&quot;</span>, <span style="color: #0000ff;">&quot;2&quot;</span>, <span style="color: #0000ff;">&quot;6&quot;</span><span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;4&quot;</span>, <span style="color: #0000ff;">&quot;5&quot;</span>, <span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>whereas sorting it on second column will give</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;4&quot;</span>, <span style="color: #0000ff;">&quot;5&quot;</span>, <span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;1&quot;</span>, <span style="color: #0000ff;">&quot;2&quot;</span>, <span style="color: #0000ff;">&quot;6&quot;</span><span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>I did not want to do the most common as well as tedious thing, i.e. write my own sorting function like this:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> myOwnSort<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> exampleArray<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//code for sorting the array according to my wish.</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>I wanted to avoid this for 2 resons:</p>
<ol>
<li>I would have to write the code for it ( lazy me!!).</li>
<li>Secondly, I can possibly never match the efficiency provided by the sorting functions of java.util.Arrays</li>
</ol>
<p><span id="more-199"></span><br />
So, my main aim was to look for a solution that would help me sort my 2-D array using the internal JAVA sorters.</p>
<p>I started digging into the JAVA docs (I must admit, I was a naive in JAVA at this point), and came across the <a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html">Comparator</a> Interface, using which we can tell the JAVA sorting function (<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#sort(java.lang.Object[], java.util.Comparator)">Arrays.sort</a>) on how to compare two elements of a 2D array. One must understand that a 2D array is essentially a 1D array with its each element as another 1D array.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> testString <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;1&quot;</span>, <span style="color: #0000ff;">&quot;2&quot;</span>, <span style="color: #0000ff;">&quot;6&quot;</span><span style="color: #009900;">&#125;</span>,<br />
&nbsp; &nbsp; <span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;4&quot;</span>, <span style="color: #0000ff;">&quot;5&quot;</span>, <span style="color: #0000ff;">&quot;3&quot;</span><span style="color: #009900;">&#125;</span><br />
&nbsp; <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></div></div>
<p>Above, there are two arrays containing 3 strings each. We must now compare these two arrays to determine their order in the resultant 2D array. We can compare these two 1-D arrays in any way we like, we can compare them on the first column, third column or on the average length of the strings (just anything!!). Let us implement Comparator interface to define such a compare function:</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">class</span> 2DcolumnComparator <span style="color: #000000; font-weight: bold;">implements</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Acomparator+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Comparator</span></a> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> columnToSortOn<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//contructor to set the column to sort on.</span><br />
&nbsp; &nbsp; 2DcolumnComparator<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> columnToSortOn<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">columnToSortOn</span> <span style="color: #339933;">=</span> columnToSortOn<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// Implement the abstract method which tells</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// how to order the two elements in the array.</span><br />
&nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> compare<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> o1, <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aobject+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Object</span></a> o2<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// cast the object args back to string arrays</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> row1 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>o1<span style="color: #339933;">;</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> row2 <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span>o2<span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #666666; font-style: italic;">// compare the desired column values</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #000000; font-weight: bold;">return</span> row1<span style="color: #009900;">&#91;</span>columnToSortOn<span style="color: #009900;">&#93;</span>.<span style="color: #006633;">compareTo</span><span style="color: #009900;">&#40;</span>row2<span style="color: #009900;">&#91;</span>columnToSortOn<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
&nbsp; &nbsp; <span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Above, we create a class 2DcolumnComparator which implements comparator interface and compare 1D array elements on the column passed to the constructor. Now, we can pass an instance of this class to the Arrays.sort function, and the 2D array will sorted on the desired column.</p>
<div class="codecolorer-container java default" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="java codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Analysis <span style="color: #009900;">&#123;</span><br />
<br />
&nbsp; <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a> args<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> childrenArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Astring+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">String</span></a><span style="color: #009900;">&#91;</span>MAXLIMIT<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #666666; font-style: italic;">//Populate the Array and sort</span><br />
&nbsp; &nbsp; <a href="http://www.google.com/search?hl=en&amp;q=allinurl%3Aarrays+java.sun.com&amp;btnI=I%27m%20Feeling%20Lucky"><span style="color: #003399;">Arrays</span></a>.<span style="color: #006633;">sort</span><span style="color: #009900;">&#40;</span>childrenArray,<span style="color: #000000; font-weight: bold;">new</span> 2DcolumnComparator<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span><br />
<span style="color: #009900;">&#125;</span><br />
<span style="color: #009900;">&#125;</span></div></div>
<p>Here, we instantiate the 2DcolumnComaparator class and pass the column to sort on as parameter to its constructor. Above, the 2D array will be sorted on its fifth column (starting from zero). You can, of course, change the <strong>compare</strong> function to order elements according to your wish.</p>
<p>That is all I needed to sort a 2-D Array. Happy Sorting!!! <img src='http://publicmind.in/blog/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p><strong>Useful Resources:</strong><br />
<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html">http://java.sun.com/j2se/1.4.2/docs/api/java/util/Comparator.html</a><br />
<a href="http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#sort(java.lang.Object[], java.util.Comparator)">http://java.sun.com/j2se/1.4.2/docs/api/java/util/Arrays.html#sort(java.lang.Object[], java.util.Comparator)</a></p>
<p><strong>Recommended Reading:</strong><br />
<a href="http://publicmind.in/blog/books/corejava1" target="_blank">Core Java(TM), Volume I&#8211;Fundamentals (8th Edition)</a><br />
<a href="http://publicmind.in/blog/books/corejava2" target="_blank">Core Java(TM), Volume 2&#8211;Fundamentals (8th Edition)</a>
</p>
<p>Update: If you need to sort a 2D array of objects of any class which implements Comparable, you can look at the <a href="http://publicmind.in/blog/sorting-2d-array-java/#comment-640">comment</a> by Lars. Article last updated on 25 Feb 2010.</p>
]]></content:encoded>
			<wfw:commentRss>http://publicmind.in/blog/sorting-2d-array-java/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>
