<?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>Information Retrieval Blog &#187; 排序</title>
	<atom:link href="http://blog.zye.me/tag/%e6%8e%92%e5%ba%8f/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.zye.me</link>
	<description>REAL TIME DATA PROCESSING, DISTRIBUTED COMPUTING, PATTERN DISCOVERY</description>
	<lastBuildDate>Tue, 31 Jan 2012 02:05:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>java HashMap 如何简单地排序后输出&#8211;备忘</title>
		<link>http://blog.zye.me/2011/04/3960.html</link>
		<comments>http://blog.zye.me/2011/04/3960.html#comments</comments>
		<pubDate>Sun, 24 Apr 2011 14:28:47 +0000</pubDate>
		<dc:creator>yezheng</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[HashMap]]></category>
		<category><![CDATA[排序]]></category>

		<guid isPermaLink="false">http://www.5yiso.cn/articles/java-hashmap-%e5%a6%82%e4%bd%95%e7%ae%80%e5%8d%95%e5%9c%b0%e6%8e%92%e5%ba%8f%e5%90%8e%e8%be%93%e5%87%ba-%e5%a4%87%e5%bf%98.html</guid>
		<description><![CDATA[工作中相信大家都会是HashMap记录一些映射，记录完后可能还需要对其排序后输出。下面是一简单例子，记录下以作备忘！   HashMap&#60;Term, Float&#62; termMap = new HashMap&#60;Term, Float&#62;();  List entries = new ArrayList(termMap.entrySet()); Comparator cmp = new Comparator() { public int compare(Object o1, Object o2) { Map.Entry e1 = (Map.Entry) o1; Map.Entry e2 = (Map.Entry) o2; Comparable v1 = (Comparable) e1.getValue(); Comparable v2 = (Comparable) e2.getValue(); return v2.compareTo(v1); } }; Collections.sort(entries, cmp); Iterator keys = <a href='http://blog.zye.me/2011/04/3960.html'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p><font face="Verdana"><font color="#000000" size="2">工作中相信大家都会是HashMap记录一些映射，记录完后可能还需要对其排序后输出。下面是一简单例子，记录下以作备忘！</font></font></p>
<p><font face="Verdana" size="2">  HashMap&lt;Term, Float&gt; termMap = new HashMap&lt;Term,   Float&gt;();</font><font face="Verdana">  List entries = new   ArrayList(termMap.entrySet());<br />
Comparator cmp = new Comparator()   {<br />
public int compare(Object o1, Object o2)   {<br />
Map.Entry e1 = (Map.Entry)   o1;<br />
Map.Entry e2 = (Map.Entry)   o2;<br />
Comparable v1 = (Comparable)   e1.getValue();<br />
Comparable v2 = (Comparable)   e2.getValue();<br />
return   v2.compareTo(v1);<br />
}<br />
};<br />
Collections.sort(entries,   cmp);<br />
Iterator keys = entries.iterator();<br />
int   resultCounter = 0;<br />
String queryString = &#8220;&#8221;;<br />
while   (keys.hasNext() &amp;&amp; resultCounter &lt; expandSize)   {<br />
Map.Entry en = (Map.Entry)   keys.next();<br />
Term t = new Term(((Term)   en.getKey()).field(),<br />
((Term)   en.getKey()).text());<br />
//   System.out.println(&#8220;oringe Term:&#8221; +   t);<br />
if (oringeSet.contains(t))   {<br />
System.out.println(&#8220;oringe Term:&#8221; +   t);<br />
continue;<br />
}<br />
queryString   += en.getKey() + &#8221;   &#8220;;<br />
resultCounter++;<br />
}</font></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zye.me/2011/04/3960.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>IBM Haifa Team 把Lucene Ranking系统打造成state-of-the-art ，TREC 2007 Million Queries Track &#8211; IBM Haifa Team</title>
		<link>http://blog.zye.me/2009/02/49240.html</link>
		<comments>http://blog.zye.me/2009/02/49240.html#comments</comments>
		<pubDate>Wed, 18 Feb 2009 16:54:05 +0000</pubDate>
		<dc:creator>yezheng</dc:creator>
				<category><![CDATA[information Retrieval]]></category>
		<category><![CDATA[lucene]]></category>
		<category><![CDATA[信息检索]]></category>
		<category><![CDATA[排序]]></category>

		<guid isPermaLink="false">http://blog.so8848.com/2009/02/49240.html</guid>
		<description><![CDATA[TREC 2007 Million Queries Track &#8211; IBM Haifa Team The&#160;&#160;Million Queries Track&#160;ran for the first time in 2007. Quoting from the track home page: &#8220;The goal of this track is to run a retrieval task similar to standard ad-hoc retrieval, but to evaluate large numbers ofqueries incompletely, rather than a small number more completely.Participants will <a href='http://blog.zye.me/2009/02/49240.html'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p><span class="Apple-style-span" style="border-collapse: separate; color: rgb(51, 51, 51); font-family: Verdana; font-size: 13px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: normal; orphans: 2; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: 2; word-spacing: 0px;"><br />
<h3 class="post-title entry-title" style="margin: 0px; padding: 0px; font-size: 125%; font-weight: bold; line-height: 1.1em;"><a href="http://jeffye.so8848.com/2008/05/trec-2007-million-queries-track-ibm.html" style="text-decoration: none; color: rgb(51, 51, 51); font-weight: bold;">TREC 2007 Million Queries Track &#8211; IBM Haifa Team</a></h3>
<div class="post-body entry-content" style="margin: 0px 0px 0.75em; line-height: 1.3em;">
<div xmlns="http://www.w3.org/1999/xhtml" style="margin: 0px 0px 0.75em; line-height: 1.3em;">
<p>The<span class="Apple-converted-space">&nbsp;</span><a href="http://ciir.cs.umass.edu/research/million/" rel="nofollow" class="external" style="color: rgb(51, 102, 153);"><img alt="[WWW]" src="http://wiki.apache.org/wiki/modern/img/moin-www.png" style="border: 1px solid rgb(204, 204, 204); padding: 4px;" width="11" height="11" /><span class="Apple-converted-space">&nbsp;</span>Million Queries Track</a><span class="Apple-converted-space">&nbsp;</span>ran for the first time in 2007.</p>
<p>Quoting from the track home page:</p>
<ul>
<li>
<p>&#8220;The goal of this track is to run a retrieval task similar to standard ad-hoc retrieval,</p>
<ul>
<li style="list-style-type: none;">
<p>but to evaluate large numbers of<br />queries incompletely, rather than a small number more completely.<br />Participants will run 10,000 queries and a random 1,000 or so will be<br />evaluated. The corpus is the terabyte track&#8217;s GOV2 corpus of roughly<br />25,000,000 .gov web pages, amounting to just under half a terabyte of<br />data.&#8221;</p>
</li>
</ul>
<p>We participated in this track with two search engines &#8211; our home brewed search engine<span class="Apple-converted-space">&nbsp;</span><a href="http://trec.nist.gov/pubs/trec10/papers/JuruAtTrec.pdf" rel="nofollow" class="external" style="color: rgb(51, 102, 153);"><img alt="[WWW]" src="http://wiki.apache.org/wiki/modern/img/moin-www.png" style="border: 1px solid rgb(204, 204, 204); padding: 4px;" width="11" height="11" /><span class="Apple-converted-space">&nbsp;</span>Juru</a>.</li>
</ul>
<p>The official reports and papers of the track should be available<br />sometimes in February 2008, but here is a summary of the results and<br />our experience with our first ever Lucene submission to TREC.</p>
<p>In summary, the out-of-the-box search quality was not so great, but<br />by altering how we use Lucene (that is, our application) and with some<br />modifications to Lucene, we were able to improve the search quality<br />results and to score good in this competition.</p>
<p>The lessons we learned can be of interest to applications using<br />Lucene, to Lucene itself, and to researchers submitting to other TREC<br />tracks (or elsewhere).</p>
<h2 id="head-a8490979552c8ea3e873074be458fb39b977a781" style="margin: 0px;">Training</h2>
<p>As preparation for the track runs we &#8220;trained&#8221; Lucene on queries<br />from previous years tracks &#8211; more exactly on the 150 short TREC queries<br />for which there are existing judgments from previous years, for the<br />same GOV2 data.</p>
<p>We build an index &#8211; actually 27 indexes &#8211; for this data. For<br />indexing we used the Trec-Doc-Maker that is now in Lucene&#8217;s contrib<br />benchmark (or a slight modification of it).</p>
<p>We found that best results are obtained when all data is in a single<br />field, and so we did, keeping only stems (English, Porter, from Lucene<br />contrib). We used the Standard-Analyzer, with a modified stoplist that<br />took into account that domain specific stopwords.</p>
<p>Running with both Juru and Lucene, and having obtained good results<br />with Juru in previous years, we had something to compare to. For this,<br />we made sure to HTML parse the documents in the same way in both<br />systems (we used Juru&#8217;s HTML parser for this) and use the same stoplist<br />etc.</p>
<p>In addition, anchor text was collect in a pre-indexing global<br />analysis pass, and so anchors of (pointing to) pages where indexed with<br />the page they point to, up to a limited size. The number of in-links to<br />each page was saved in a stored field and we used it as a static score<br />element (boosting documents that had more in-links). The way that<br />anchors text was extracted and prepared for indexing will be described<br />in the full report.</p>
<h2 id="head-3c69e51860bb0a192a1940b8243f91822c430b7e" style="margin: 0px;">Results</h2>
<p>The initial results were:</p>
<ul>
<li style="list-style-type: none;">
<div style="margin: 0px 0px 0.75em; line-height: 1.3em;">
<table>
<tbody></tbody>
<tbody>
<tr style="background-color: rgb(128, 255, 128);">
<td>
<p><strong>Run</strong></p>
<p></td>
<td>
<p><strong>MAP</strong></p>
<p></td>
<td>
<p><strong>P@5</strong></p>
<p></td>
<td>
<p><strong>P@10</strong></p>
<p></td>
<td>
<p><strong>P@20</strong></p>
<p></td>
</tr>
<tr>
<td>
<p>1. Juru</p>
</td>
<td>
<p>0.313</p>
</td>
<td>
<p>0.592</p>
</td>
<td>
<p>0.560</p>
</td>
<td>
<p>0.529</p>
</td>
</tr>
<tr>
<td>
<p>2. Lucene out-of-the-box</p>
<p></td>
<td>
<p>0.154</p>
<p></td>
<td>
<p>0.313</p>
<p></td>
<td>
<p>0.303</p>
<p></td>
<td>
<p>0.289</p>
<p></td>
</tr>
<tr>
<td valign="top"></td>
<td valign="top"></td>
<td valign="top"></td>
<td valign="top"></td>
<td valign="top"></td>
</tr>
</tbody>
</table>
<p><b>We made the following changes:</b></div>
</li>
</ul>
<ol type="1">
<li>
<p>Add a proximity scoring element, basing on our experience with &#8220;Lexical affinities&#8221; in Juru.</p>
<ul>
<li style="list-style-type: none;">
<p>Juru creates posting lists for lexical affinities. In Lucene we used augmented the query with Span-Near-Queries.</p>
</li>
</ul>
</li>
<li>
<p>Phrase expansion &#8211; the query text was added to the query as a phrase.</p>
</li>
<li>
<p>Replace the default similarity by Sweet-Spot-Similarity for a better</p>
<ul>
<li style="list-style-type: none;">
<p>choice of document length normalization. Juru is using<span class="Apple-converted-space">&nbsp;</span><a href="http://citeseer.ist.psu.edu/singhal96pivoted.html" rel="nofollow" class="external" style="color: rgb(51, 102, 153);"><img alt="[WWW]" src="http://wiki.apache.org/wiki/modern/img/moin-www.png" style="border: 1px solid rgb(204, 204, 204); padding: 4px;" width="11" height="11" /><span class="Apple-converted-space">&nbsp;</span>pivoted length normalization</a><span class="Apple-converted-space">&nbsp;</span>and we experimented with it, but found out that the simpler and faster sweet-spot-simiarity performs better.</p>
</li>
</ul>
</li>
<li>
<p>Normalized term-frequency, as in Juru.</p>
<ul>
<li style="list-style-type: none;">
<p>Here, tf(freq) is normalized by the average term frequency of the document.</p>
</li>
</ul>
</li>
</ol>
<p>So these are the updated results:</p>
<ul>
<li style="list-style-type: none;">
<div style="margin: 0px 0px 0.75em; line-height: 1.3em;">
<table width="505" height="264">
<tbody></tbody>
<tbody>
<tr style="background-color: rgb(128, 255, 128);">
<td>
<p><strong>Run</strong></p>
<p></td>
<td>
<p><strong>MAP</strong></p>
<p></td>
<td>
<p><strong>P@5</strong></p>
<p></td>
<td>
<p><strong>P@10</strong></p>
<p></td>
<td valign="top"></td>
<td>
<p><strong>P@20</strong></p>
<p></td>
</tr>
<tr>
<td>
<p>1. Juru</p>
</td>
<td>
<p>0.313</p>
</td>
<td>
<p>0.592</p>
</td>
<td>
<p>0.560</p>
</td>
<td valign="top"></td>
<td>
<p>0.529</p>
</td>
</tr>
<tr>
<td>
<p>2. Lucene out-of-the-box</p>
</td>
<td>
<p>0.154</p>
</td>
<td>
<p>0.313</p>
</td>
<td>
<p>0.303</p>
</td>
<td valign="top"></td>
<td>
<p>0.289</p>
</td>
</tr>
<tr>
<td>
<p>3. Lucene + LA + Phrase + Sweet Spot + tf-norm</p>
</td>
<td>
<p>0.306</p>
</td>
<td>
<p>0.627</p>
</td>
<td>
<p>0.589</p>
</td>
<td valign="top"></td>
<td>
<p>0.543</p>
</td>
</tr>
</tbody>
</table>
<p></div>
</li>
</ul>
<p><b>The improvement is dramatic.</b></p>
<p<br />
>Perhaps even more important, once the track results were published,<br />we found out that these improvement are consistent and steady, and so<br />Lucene with these changes was ranked high also by the two new measures<br />introduced in this track &#8211; NEU-Map and E-Map (Epsilon-Map).</p>
<p>With these new measures more queries are evaluated but less<br />documents are judged for each query. The algorithms for documents<br />selection for judging (during the evaluation stage of the track) were<br />not our focus in this work &#8211; as there were actually two goals to this<br />TREC:</p>
<ul>
<li>
<p>the systems evaluation (our main goal) and</p>
</li>
<li>
<p>the evaluation itself.</p>
</li>
</ul>
<p>The fact that modified Lucene scored well in both the traditional<br />150 queries and the new 1700 evaluated queries with the new measures<br />was reassuring for the &#8220;usefulness&#8221; or perhaps &#8220;validity&#8221; of these<br />modifications to Lucene.</p>
<p>For certain these changes are not a 100% fit for every application<br />and every data, but these results are strong, and so I believe can be<br />be valuable for many applications, and certainly for research aspects.</p>
<h2 id="head-0eb636cbe7c87b327baff095eb8bdf4821605b99" style="margin: 0px;">Search time penalty</h2>
<p>
<p>These improvements did not come for free. Adding a phrase to the<br />query and adding Span-Near-Queries for every pair of query words costs<br />query time.</p>
<p>The search time of stock Lucene in our setup was 1.4 seconds/query.<br />The modified search time took 8.0 seconds/query. This is a large<br />slowdown!</p>
<p>But it should be noticed that in this work we did not focus in<br />search time, only in quality. Now is the time to see how the search<br />time penalty can be reduced while keeping most of the search time<br />improvements.</p>
<p>
<h2 id="head-ff042c1c30571c507b178275480968709f3edd85" style="margin: 0px;">Implementation Details</h2>
<p>
<ul>
<li>
<p>Contrib benchmark quality package was used for the search quality measures and submissions.</p>
</li>
<li>
<p>In order to experiment with various length normalizations the<br />most straight forward way would have been to create a separate index<br />for each option. But this was unreasonable for the amount of data, and<br />for the flexibility that was required in order to try new things. So we<br />indexed and searched like this:</p>
<ul>
<li>
<p>Omit norms for the (single) text field.</p>
</li>
<li>
<p>index document length (number of terms) in a dedicated field.</p>
</li>
<li>
<p>index document unique length (number of unique terms) is another dedicated field.</p>
</li>
<li>
<p>index number of in-links (see anchors above) in a third dedicated field.</p>
</li>
<li>
<p>Create an !indexReader that implements norms() by reading (caching) the document length field and either:</p>
<ul>
<li>
<p>Imitate the stock Lucene by normalizing the length with DefaultSimilarity and then compressing it to a single byte.</p>
</li>
<li>
<p>Similarly imitate SweetSpot similarity.</p>
</li>
</ul>
</li>
<li>
<p>For pivoted length normalization tests (which are not listed in<br />the results below because they were outperformed with the simpler<br />sweet-spot similarity) we used a regular index reader (so norms were 1)<br />and used a CustomScoreQuery (search.function) with a ValueSourceQuery<br />part that did the normalization &#8211; it read the unique length field and<br />used the average on it to compute the pivoted norm.</p>
</li>
<li>
<p>In-links static rank scoring was also implemented with<br />CustomScoreQuery &#8211; with a FieldScoreQuery that read the cached in-links<br />count.</p>
</li>
</ul>
<ul>
<li>
<p>At some point I tried to accelerate the search and improve its<br />quality by creating a new query &#8211; an OR with Phrase with Proximity<br />query. This query should have been faster (I hoped) because it read<br />each posting just once. This is in oppose to creating a SpanNear query<br />for each pair of query words, in addition to a phrase query and an or<br />query. But the results were very disappointing: search time was not<br />improved, and quality was hurt. Might just be a bug&#8230; But I learned to<br />appreciate even more the modularity of Lucene queries.</p>
</li>
</ul>
</li>
</ul>
<p>Here is an example Lucene Query. For the input query (topic):<span class="Apple-converted-space"> </span></p>
<pre>     U.S. oil industry history&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;p&gt;the following Lucene query was created: &lt;br /&gt;&lt;/p&gt;&lt;br /&gt;&lt;pre&gt;  oil industri histori&lt;br /&gt;  (&lt;br /&gt;    spanNear([oil, industri], 8, false)&lt;br /&gt;    spanNear([oil, histori], 8, false)&lt;br /&gt;    spanNear([industri, histori], 8, false)&lt;br /&gt;  )^4.0&lt;br /&gt;  "oil industri histori"~1^0.75&lt;br /&gt;</pre>
<p>This demonstrates that:</p>
<ul>
<li>
<p>U.S. is considered a stop word and was removed from the query text.</p>
</li>
<li>
<p>Only stemmed forms of words are used.</p>
</li>
<li>
<p>Default query operator is OR.</p>
</li>
<li>
<p>Words found in a document up to 7 positions apart form a lexical affinity. (8 in this example because of the stopped word.)</p>
</li>
<li>
<p>Lexical affinity matches are boosted 4 times more than single word matches.</p>
</li>
<li>
<p>Phrase matches are counted slightly less than single word matches.</p>
</li>
<li>
<p>Phrases allow fuzziness when words were stopped.</p>
</li>
</ul>
<p><img alt="/!" src="http://wiki.apache.org/wiki/modern/img/alert.png" style="border: 1px solid rgb(204, 204, 204); padding: 4px;" width="15" height="15" /><span class="Apple-converted-space">&nbsp;</span>(1) todo: refer to payloads (2) todo: describe tf normalization implementation.</p>
</div>
</div>
<p></span></p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zye.me/2009/02/49240.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>关于修改/增加lucene排序算法的讨论</title>
		<link>http://blog.zye.me/2008/07/36256.html</link>
		<comments>http://blog.zye.me/2008/07/36256.html#comments</comments>
		<pubDate>Tue, 01 Jul 2008 14:53:55 +0000</pubDate>
		<dc:creator>yezheng</dc:creator>
				<category><![CDATA[information Retrieval]]></category>
		<category><![CDATA[lucene]]></category>
		<category><![CDATA[ranking]]></category>
		<category><![CDATA[信息检索]]></category>
		<category><![CDATA[排序]]></category>

		<guid isPermaLink="false">http://www.5yiso.cn/2008/07/36256.html</guid>
		<description><![CDATA[http://www.mail-archive.com/general@lucene.apache.org/msg00431.html http://www.mail-archive.com/general@lucene.apache.org/msg00432.html 由于刚读研的时候就开始学习Lucene，所以一直对Lucene情有独钟，现在想在排序ranking方面做些研究，感觉最好先能花点时间把一些基本ranking算法实现，譬如tfidf，bm25什么的。想在lucene框架的基础上完成这个工作，但研究一段感觉有困难，今天看到上面链接里的帖子也遇到这样的问题，不知有没有大侠有过相关研究，还劳烦给点提示！ 其实最开始是想实现一个Lab-Lucene，用于做各种IR相关实验，但一开始就被这个问题给难住了，挺郁闷。以后有进展，定分享之。 &#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211; PS:  2010-02-28 NEWS： Lab-Lucene 现在主要已经基本开发完，现在已经包括绝大多少basic weighting model (LM, BM25, DFR, TFIDF) , 以及各种Query Expansion Models, 性能在一些列 TREC ad hoc datasets 上，至少与公开实验数据是comparable. 计划在写完基于hadoop 分布式所以和检索后开源，当然还有文档。欢迎讨论！]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.mail-archive.com/general@lucene.apache.org/msg00431.html">http://www.mail-archive.com/general@lucene.apache.org/msg00431.html</a></p>
<p>http://www.mail-archive.com/general@lucene.apache.org/msg00432.html</p>
<p>由于刚读研的时候就开始学习Lucene，所以一直对Lucene情有独钟，现在想在排序ranking方面做些研究，感觉最好先能花点时间把一些基本ranking算法实现，譬如tfidf，bm25什么的。想在lucene框架的基础上完成这个工作，但研究一段感觉有困难，今天看到上面链接里的帖子也遇到这样的问题，不知有没有大侠有过相关研究，还劳烦给点提示！</p>
<p>其实最开始是想实现一个Lab-Lucene，用于做各种IR相关实验，但一开始就被这个问题给难住了，挺郁闷。以后有进展，定分享之。</p>
<p>&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8212;&#8211;</p>
<p>PS:  2010-02-28</p>
<p>NEWS： Lab-Lucene 现在主要已经基本开发完，现在已经包括绝大多少basic weighting model (LM, BM25, DFR, TFIDF) , 以及各种Query Expansion Models, 性能在一些列 TREC ad hoc datasets 上，至少与公开实验数据是comparable.</p>
<p>计划在写完基于hadoop 分布式所以和检索后开源，当然还有文档。欢迎讨论！</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zye.me/2008/07/36256.html/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>java 中排序方法（转）&#8211;备忘系列</title>
		<link>http://blog.zye.me/2007/02/6006.html</link>
		<comments>http://blog.zye.me/2007/02/6006.html#comments</comments>
		<pubDate>Thu, 22 Feb 2007 07:05:00 +0000</pubDate>
		<dc:creator>yezheng</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[信息检索]]></category>
		<category><![CDATA[备忘]]></category>
		<category><![CDATA[排序]]></category>

		<guid isPermaLink="false">http://www.5yiso.cn/2007/02/6006.html</guid>
		<description><![CDATA[简单类型的排序 简单类型不外是byte, char, short, int, long, float, double等数据类型， 这些类型不能放在聚集中，只能使用数组。java.util.Arrays方法提供了对这些类型的sort方法（实际上还有很多其他有用的方法），下面是对一个简单的int数组排序： int[] arr = {2, 3, 1,10,7,4}; System.out.print(&#8220;before sort: &#8220;); for (int i = 0; i&#60; arr.length; i++) System.out.print(arr[i] + &#8221; &#8220;); System.out.println(); Arrays.sort(arr); System.out.print(&#8220;after sort: &#8220;); for (int i = 0; i&#60; arr.length; i++) System.out.print(arr[i] + &#8221; &#8220;); System.out.println(); 输出结果： before sort: 2 3 1 10 <a href='http://blog.zye.me/2007/02/6006.html'>[...]</a>]]></description>
			<content:encoded><![CDATA[<p><strong>简单类型的排序</strong></p>
<p style="MARGIN: 0cm 0cm 0pt">简单类型不外是byte, char, short, int, long, float, double等数据类型， 这些类型不能放在聚集中，只能使用数组。java.util.Arrays方法提供了对这些类型的sort方法（实际上还有很多其他有用的方法），下面是对一个简单的int数组排序：</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>int</strong>[] arr = {2, 3, 1,10,7,4};</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;before sort: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; arr.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(arr[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Arrays.sort(arr);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;after sort: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; arr.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(arr[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt">输出结果：</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">before sort: 2 3 1 10 7 4</p>
<p style="MARGIN: 0cm 0cm 0pt">after sort: 1 2 3 4 7 10</p>
<p style="MARGIN: 0cm 0cm 0pt">我们看到排序结果是按照升序排列的，下面的排序都是如此。</p>
<p style="MARGIN: 0cm 0cm 0pt">
<p style="MARGIN: 0cm 0cm 0pt"><strong>对象的排序</strong></p>
<p style="MARGIN: 0cm 0cm 0pt">对象可以放在数组里，同样调用Arrays.sort(Object[] arr)即可；也可以放到聚集里，用java.util.Collections的sort(List list)。注意不是list必须实现List接口而不仅仅是Collection接口。</p>
<p style="MARGIN: 0cm 0cm 0pt">但是这个类必须实现了java.lang.Comparable接口。这个接口只有一个方法：int compartTo(Object o)，当本对象比传入的对象大时，返回一个正整数。 以类Programmer为例：</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>class</strong> Programmer <strong>implements</strong> Comparable{</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>private</strong> String name;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>private</strong> String language;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>private</strong> <strong>double</strong> pay;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> Programmer(String name, String language, <strong>double</strong> pay) {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>this</strong>.name = name;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>this</strong>.language = language;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>this</strong>.pay = pay;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>int</strong> compareTo(Object o) {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Programmer other = (Programmer)o;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> (<strong>int</strong>)pay &#8211; (<strong>int</strong>)other.pay;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> String toString(){</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> &#8220;{name: &#8221; + name + &#8220;, language: &#8221; + language + &#8220;, money: &#8221; + pay + &#8220;}&#8221;;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt">对其进行排序：</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">ArrayList list = <strong>new</strong> ArrayList();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">list.add(<strong>new</strong> Programmer(&#8220;张三&#8221;, &#8220;C&#8221;, 12000));</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">list.add(<strong>new</strong> Programmer(&#8220;李四&#8221;, &#8220;Java&#8221;, 200));</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">list.add(<strong>new</strong> Programmer(&#8220;王五&#8221;, &#8220;C++&#8221;, 5000));</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">list.add(<strong>new</strong> Programmer(&#8220;钱六&#8221;, &#8220;VB&#8221;, 3000));</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println(&#8220;before sort: &#8221; + list);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Collections.sort(list);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println(&#8220;after sort: &#8221; + list);</p>
<p style="MARGIN: 0cm 0cm 0pt">输出：</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">before sort: [{name: 张三, language: C, money: 12000.0}, {name: 李四, language: Java, money: 200.0}, {name: 王五, language: C++, money: 5000.0}, {name: 钱六, language: VB, money: 3000.0}]</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">after sort: [{name: 李四, language: Java, money: 200.0}, {name: 钱六, language: VB, money: 3000.0}, {name: 王五, language: C++, money: 5000.0}, {name: 张三, language: C, money: 12000.0}]</p>
<p style="MARGIN: 0cm 0cm 0pt">
<p style="MARGIN: 0cm 0cm 0pt">够简单吧！查查Comparable的javadoc可以知道，有很多类已经实现了该接口，因此对这些类的排序几行代码就可以搞定。</p>
<p style="MARGIN: 0cm 0cm 0pt">最近看C#发现其中用System.Array.sort对数组排序，适用于所有实现了IComparable接口的对象，看来微软的借鉴能力还真是强啊！</p>
<p style="MARGIN: 0cm 0cm 0pt">
<p style="MARGIN: 0cm 0cm 0pt"><strong>对已有类进行排序</strong></p>
<p style="MARGIN: 0cm 0cm 0pt">上面的方法有一个问题，就是一个类已经存在了，并且没有实现Comparable接口，使用一个子类进行封装？很麻烦（你可以对下面的例子试试）。还有一种情况就是对一个类没法实现多种排序。以File类为例，它实现了Comparable接口，但是是按照名称排序的。如果要按照大小排序，或者按修改时间排序呢？对这两种情况，使用java.util包的Comparator接口：</p>
<p style="MARGIN: 0cm 0cm 0pt">Arrays.sort(Object[] arr, Comparator com)</p>
<p style="MARGIN: 0cm 0cm 0pt">Collections.sort(Object[] arr, Comparator com)</p>
<p style="MARGIN: 0cm 0cm 0pt">Comparator接口的方法：</p>
<p style="MARGIN: 0cm 0cm 0pt"><strong>public</strong> <strong>int</strong> compare(Object o1, Object o2) 当o1比o2大时返回一个正整数</p>
<p style="MARGIN: 0cm 0cm 0pt"><strong>public</strong> <strong>boolean</strong> equals(Object obj) 判断obj与这个Comparator是否同一个对象</p>
<p style="MARGIN: 0cm 0cm 0pt">下面使用Comparator对文件实现了按文件大小或修改时间排序：</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>class</strong> FileUtils {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>static</strong> <strong>class</strong> CompratorByLastModified <strong>implements</strong> Comparator {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>int</strong> compare(Object o1, Object o2) {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File file1 = (File)o1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File file2 = (File)o2;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>long</strong> diff = file1.lastModified() &#8211; file2.lastModified();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>if</strong> (diff &gt; 0)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> 1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>else</strong> <strong>if</strong> (<br />
diff == 0)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> 0;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>else</strong></p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> -1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>boolean</strong> equals(Object obj){</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> <strong>true</strong>; //简单做法</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>static</strong> <strong>class</strong> CompratorBySize <strong>implements</strong> Comparator {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>int</strong> compare(Object o1, Object o2) {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File file1 = (File)o1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File file2 = (File)o2;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>long</strong> diff = file1.length() &#8211; file2.length();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>if</strong> (diff &gt; 0)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> 1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>else</strong> <strong>if</strong> (diff == 0)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> 0;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>else</strong></p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> -1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>boolean</strong> equals(Object obj){</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> <strong>true</strong>; //简单做法</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt">}</p>
<p style="MARGIN: 0cm 0cm 0pt">调用的示例：</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File dir = <strong>new</strong> File(&#8220;C:\temp&#8221;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File[] files = dir.listFiles();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;before sort: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; files.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(files[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Arrays.sort(files);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;sort by name: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; files.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(files[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Arrays.sort(files, <strong>new</strong> FileUtils.CompratorBySize());</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;sort by size: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; files.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(files[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Arrays.sort(files, <strong>new</strong> FileUtils.CompratorByLastModified());</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;sort by last modified: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; files.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(files[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt">自己找个目录试一下吧。用这些Java类库中的方法，一般情况下应该是不用自己写排序算法了吧？</p>
<p style="MARGIN: 0cm 0cm 0pt">
<p style="MARGIN: 0cm 0cm 0pt">最后附上完整代码占点版面：</p>
<p style="MARGIN: 0cm 0cm 0pt">TestSort.java</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>import</strong> java.io.*;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>import</strong> java.util.*;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>class</strong> TestSort {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>static</strong> <strong>void</strong> main(String[] args) {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">sortSimpleType();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">sortComparable();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">sortComparator();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>static</strong> <strong>void</strong> sortSimpleType() {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>int</strong>[] arr = {2, 3, 1,10,7,4};</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;before sort: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; arr.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(arr[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Arrays.sort(arr);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;after sort: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; arr.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(arr[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>static</strong> <strong>void</strong> sortComparable() {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">ArrayList list = <strong>new</strong> ArrayList();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">list.add(<strong>new</strong> Programmer(&#8220;张三&#8221;, &#8220;C&#8221;, 12000));</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">list.add(<strong>new</strong> Programmer(&#8220;李四&#8221;, &#8220;Java&#8221;, 200));</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">list.add(<strong>new</strong> Programmer(&#8220;王五&#8221;, &#8220;C++&#8221;, 5000));</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">list.add(<strong>new</strong> Programmer(&#8220;钱六&#8221;, &#8220;VB&#8221;, 3000));</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println(&#8220;before sort: &#8221; + list);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Collections.sort(list);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println(&#8220;after sort: &#8221;<br />
 + list);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>static</strong> <strong>void</strong> sortComparator() {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File dir = <strong>new</strong> File(&#8220;C:\temp&#8221;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File[] files = dir.listFiles();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;before sort: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; files.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(files[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Arrays.sort(files);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;sort by name: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; files.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(files[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Arrays.sort(files, <strong>new</strong> FileUtils.CompratorBySize());</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;sort by size: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; files.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(files[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Arrays.sort(files, <strong>new</strong> FileUtils.CompratorByLastModified());</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(&#8220;sort by last modified: &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>for</strong> (<strong>int</strong> i = 0; i&lt; files.length; i++)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.print(files[i] + &#8221; &#8220;);</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">System.out.println();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>class</strong> Programmer <strong>implements</strong> Comparable{</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>private</strong> String name;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>private</strong> String language;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>private</strong> <strong>double</strong> pay;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> Programmer(String name, String language, <strong>double</strong> pay) {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>this</strong>.name = name;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>this</strong>.language = language;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>this</strong>.pay = pay;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>int</strong> compareTo(Object o) {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">Programmer other = (Programmer)o;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> (<strong>int</strong>)pay &#8211; (<strong>int</strong>)other.pay;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> String toString(){</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> &#8220;{name: &#8221; + name + &#8220;, language: &#8221; + language + &#8220;, money: &#8221; + pay + &#8220;}&#8221;;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>class</strong> FileUtils {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>static</strong> <strong>class</strong> CompratorByLastModified <strong>implements</strong> Comparator {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>int</strong> compare(Object o1, Object o2) {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File file1 = (File)o1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File file2 = (File)o2;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>long</strong> diff = file1.lastModified() &#8211; file2.lastModified();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>if</strong> (diff &gt; 0)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> 1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>else</strong> <strong>if</strong> (diff == 0)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> 0;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>else</strong></p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> -1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>boolean</strong> equals(Object obj){</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> <strong>true</strong>; //简单做法</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>static</strong> <strong>class</strong> CompratorBySize <strong>implements</strong> Comparator {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>int</strong> compare(Object o1, Object o2) {</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File file1 = (File)o1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">File file2 = (File)o2;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>long</strong> diff = file1.length() &#8211; file2.length();</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>if</strong> (diff &gt; 0)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> 1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>else</strong> <strong>if</strong> (diff == 0)</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> 0;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>else</strong></p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> -1;</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>public</strong> <strong>boolean</strong> equals(Object obj){</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left"><strong>return</strong> <strong>true</strong>; //简单做法</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">}</p>
<p style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: left">
<p style="MARGIN: 0cm 0cm 0pt">}</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.zye.me/2007/02/6006.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

