<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
	<channel>
		<title><![CDATA[JavaRanch: Latest posts for the topic "Second order sorting using Comparable Interface"]]></title>
		<link>http://www.coderanch.com/forums/t/33/Beginning-Java/Second-order-sorting-using-Comparable</link>
		<description><![CDATA[Latest messages posted in the topic "Second order sorting using Comparable Interface"]]></description>
		<generator>JForum - http://www.jforum.net</generator>
			<item>
				<title>Second order sorting using Comparable Interface</title>
				<description><![CDATA[Hi,<br />   I have the following requirement. I need to do the sorting functionality. This the sql <br /> <br /> select * from Person <br /> order by name asc, time desc<br /> <br /> This is the code<br /> <pre>  
public class BinForm  implements Comparable
{
    public String name;
    public Date receivedDate;
    public static int sortBy;
    
    // get set methods
    
    public void setName(String name)
    {
    	this.name=name;
    }
    
   
    public String getName()
    {
       return this.name;
    }
   
   
   public Date getReceivedDate() 
   {
   	return receivedDate;
   }
   	
   	
   public void setReceivedDate(Date receivedDate) 
   {
   	this.receivedDate = receivedDate;
   }


public int compareTo(Object bimForm) throws ClassCastException 
{
	    if (!(bimForm instanceof BinaryImgTransferForm))
	      throw new ClassCastException(&quot;A BinaryImgTransferForm  object expected.&quot;);
	    BinForm bimSortObj=(BinForm) bimForm;
	    switch(sortBy)
		{
			case 1:
				return this.name.compareTo(bimSortObj.name);
			case 2:	
				return this.receivedDate.compareTo(bimSortObj.receivedDate);

			default:
				return this.receivedDate.compareTo(bimSortObj.receivedDate);
			
		}		
}


// In the servlet,
BinForm.sortBy=1
Collections.sort(bimList);

  </pre><br /> <br /> i dont know how to do second order sorting (time desc) using comparable interface.<br /> <br /> THanks<br /> Sudha]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450483/2005082</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450483/2005082</link>
				<pubDate><![CDATA[Fri, Jun 19 2009 10:32:50 MDT]]></pubDate>
				<author><![CDATA[sudha swami]]></author>
			</item>
			<item>
				<title>Second order sorting using Comparable Interface</title>
				<description><![CDATA[Perhaps something like this. Sorts first by height, and then by name.<br /> <pre>
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class Human {
	private final String name;
	private final int height;

	public String getName() {
		return name;
	}

	public Human(String name, int height) {
		super();
		this.name = name;
		this.height = height;
	}
	
	public String toString(){
		return getName() + &quot; is &quot; + getHeight() + &quot; cm tall.&quot;;
	}

	public static final Comparator&lt;Human&gt; HEIGHT_NAME = new Comparator&lt;Human&gt;() {

		public int compare(Human h1, Human h2) {
			int i = h1.getHeight() - h2.getHeight();
			if (i == 0) {
				i = h1.getName().compareToIgnoreCase(h2.getName());
			}
			return i;
		}
	};

	public int getHeight() {
		return height;
	}

	public static void main(String[] args) {
		List&lt;Human&gt; l = new ArrayList&lt;Human&gt;();
		l.add(new Human(&quot;Arnold&quot; , 100));
		l.add(new Human(&quot;Mark&quot;   , 100));
		l.add(new Human(&quot;Walter&quot;,  100));
		l.add(new Human(&quot;Anne&quot;   , 120));
		l.add(new Human(&quot;Mary&quot;   ,  90));
		l.add(new Human(&quot;Wilma&quot;  , 100));
		System.out.println(&quot;Before sort: &quot; + l);
		Collections.sort(l, Human.HEIGHT_NAME);
		System.out.println(&quot;After sort: &quot; + l);
	}
}</pre><br /> <br /> Output:<br /> <pre> 
Before sort: [Arnold is 100 cm tall., Mark is 100 cm tall., Walter is 100 cm tall., Anne is 120 cm tall., Mary is 90 cm tall., Wilma is 100 cm tall.]
After sort:  [Mary is 90 cm tall., Arnold is 100 cm tall., Mark is 100 cm tall., Walter is 100 cm tall., Wilma is 100 cm tall., Anne is 120 cm tall.]</pre>]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450483/2005215</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450483/2005215</link>
				<pubDate><![CDATA[Fri, Jun 19 2009 14:34:56 MDT]]></pubDate>
				<author><![CDATA[Max Rahder]]></author>
			</item>
			<item>
				<title>Second order sorting using Comparable Interface</title>
				<description><![CDATA[<blockquote>
			<div>
				<cite>Max Rahder wrote:</cite><pre>int i = h1.getHeight() - h2.getHeight();</pre></div>
		</blockquote><br /> Just a word of caution: using simple subtraction for ints can get very nasty if the ints get quite big. Consider the case where h1.getHeight() is Integer.MIN_VALUE, and h2.getHeight() is positive. The result will overflow, and produce nasty results.<br /> <br /> If the chances of this exist, the following is much safer (and also applies to long):<br /> <pre>int i = (h1.getHeight() &lt; h2.getHeight() ? -1 : h1.getHeight() &gt; h2.getHeight() ? 1 : 0);</pre>But if you know (and can guarantee) that the int values won't get that big, subtracting is not a problem.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450483/2005224</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450483/2005224</link>
				<pubDate><![CDATA[Fri, Jun 19 2009 15:11:27 MDT]]></pubDate>
				<author><![CDATA[Rob Prime]]></author>
			</item>
			<item>
				<title>Second order sorting using Comparable Interface</title>
				<description><![CDATA[Hi,<br /> <br />  Thanks for the information.  The above example has height in ascending order and the name is also in the ascending order.<br /> My requirement is : Height should be in the ascending order and name should be in descending order. <br /> Thanks<br /> Sudha]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450483/2006903</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450483/2006903</link>
				<pubDate><![CDATA[Mon, Jun 22 2009 09:05:45 MDT]]></pubDate>
				<author><![CDATA[sudha swami]]></author>
			</item>
			<item>
				<title>Second order sorting using Comparable Interface</title>
				<description><![CDATA[<blockquote>
			<div>
				<cite>sudha swami wrote:</cite>Hi, Thanks for the information.  The above example has height in ascending order and the name is also in the ascending order.<br /> My requirement is : Height should be in the ascending order and name should be in descending order. <br /> ThanksSudha</div>
		</blockquote><br /> <br /> I just gave you an example. You'll have to figure out how to apply it to your situation. Experiment a little.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450483/2006904</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450483/2006904</link>
				<pubDate><![CDATA[Mon, Jun 22 2009 09:08:51 MDT]]></pubDate>
				<author><![CDATA[Max Rahder]]></author>
			</item>
			<item>
				<title>Second order sorting using Comparable Interface</title>
				<description><![CDATA[I tried it the same way as you did since last few days but i couldnt figure out whethere there is an option to do one column as ascending and the other one as descending]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450483/2006923</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450483/2006923</link>
				<pubDate><![CDATA[Mon, Jun 22 2009 09:35:27 MDT]]></pubDate>
				<author><![CDATA[sudha swami]]></author>
			</item>
			<item>
				<title>Second order sorting using Comparable Interface</title>
				<description><![CDATA[For descending, all you need to do is flip the sign (e.g. -13 becomes 13, or 481 becomes -481). You can just return -(previous return value) except when the previous return value is Integer.MIN_VALUE.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450483/2006924</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450483/2006924</link>
				<pubDate><![CDATA[Mon, Jun 22 2009 09:37:51 MDT]]></pubDate>
				<author><![CDATA[Rob Prime]]></author>
			</item>
			<item>
				<title>Second order sorting using Comparable Interface</title>
				<description><![CDATA[Rob,<br /> I implemented the code changes according to your suggestion(return -(previous return value) ) and it is working fine.<br /> Thanks for your suggestion. But i dont understand <br /> <br /> "You can just return -(previous return value) except when the previous return value is Integer.MIN_VALUE " <br /> <br /> Thanks <br /> Sudha<br />  <br />  <br /> <br /> <br /> ]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450483/2007042</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450483/2007042</link>
				<pubDate><![CDATA[Mon, Jun 22 2009 13:12:23 MDT]]></pubDate>
				<author><![CDATA[sudha swami]]></author>
			</item>
			<item>
				<title>Second order sorting using Comparable Interface</title>
				<description><![CDATA[Integer.MIN_VALUE is -2,147,483,648, so -Integer.MIN_VALUE would be 2,147,483,648 - that is Integer.MAX_VALUE + 1. Since this is larger than Integer.MAX_VALUE, it wraps and... becomes Integer.MIN_VALUE again!<br /> <br /> Byte.MIN_VALUE, Short.MIN_VALUE and Long.MIN_VALUE have the same issue.]]></description>
				<guid isPermaLink="true">http://www.coderanch.com/forums/posts/preList/450483/2007070</guid>
				<link>http://www.coderanch.com/forums/posts/preList/450483/2007070</link>
				<pubDate><![CDATA[Mon, Jun 22 2009 14:08:32 MDT]]></pubDate>
				<author><![CDATA[Rob Prime]]></author>
			</item>
	</channel>
</rss>
