<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0">
<channel>
<title><![CDATA[BIWEB开源PHP WMS系统创始人ArthurXF肖飞的blog]]></title> 
<link>http://www.bizeway.net/index.php</link> 
<description><![CDATA[网务通 - 网务公司发展之路]]></description> 
<language>zh-cn</language> 
<copyright><![CDATA[BIWEB开源PHP WMS系统创始人ArthurXF肖飞的blog]]></copyright>
<item>
<link>http://www.bizeway.net/read.php?</link>
<title><![CDATA[JavaScript：多维数组像ORDER BY一样实现多键值排序 ]]></title> 
<author>ArthurXF &lt;arthurxf@gmail.com&gt;</author>
<category><![CDATA[Javascript]]></category>
<pubDate>Mon, 05 Nov 2012 05:28:48 +0000</pubDate> 
<guid>http://www.bizeway.net/read.php?</guid> 
<description>
<![CDATA[ 
	<p>JavaScript的数组排序函数 sort方法，默认是按照ASCII 字符顺序进行升序排列。</p><p><span style="font-family: Courier New">arrayobj.<strong>sort(</strong>sortfunction<strong>);</strong></span></p><p class="dt"><em>参数：sortFunction</em></p><p>可选项。是用来确定元素顺序的函数的名称。如果这个参数被省略，那么元素将按照 ASCII 字符顺序进行升序排列。</p><p><strong>sort</strong> 方法将 <strong>Array</strong> 对象进行适当的排序；在执行过程中并不会创建新的 <strong>Array</strong> 对象。 </p><p>如果为 <em>sortfunction</em> 参数提供了一个函数，那么该函数必须返回下列值之一： </p><ul><li>负值，如果所传递的第一个参数比第二个参数小。 </li><li>零，如果两个参数相等。 </li><li>正值，如果第一个参数比第二个参数大。 </li></ul><p>以上的方法在一维的排序还是很方便的，但像SQL语句中的ORDER BY 一样的多键值排序由怎么做呢？</p><p>多维数组的多键值排序，则需要复杂一些，但不需要用循环解决。实际解决的道理是一样的 。</p><p><strong>数字：</strong></p><p>以下的例子是将数字的多维数组按照第5列，第9列，第3列的顺序排序，像SQL语句中的ORDER BY &nbsp;col5，col9，col7。数字的时候可以直接两个项目相减，以结果作为返回值即可。</p><div style="background: #e6e6e6; padding: 4px 5.4pt; border: 0.5pt solid windowtext; width: 95%; word-break: break-all"><div><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" border="0" align="top" /><span style="color: #000000">&lt;</span><span style="color: #000000">script language</span><span style="color: #000000">=</span><span style="color: #000000">javascript</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" border="0" align="top" /> &nbsp; &nbsp;var myArray </span><span style="color: #000000">=</span><span style="color: #000000"> </span><span style="color: #0000ff">new</span><span style="color: #000000"> Array();<br /><img id="_80_528_Open_Image" style="display: inline" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" border="0" align="top" /><img id="_80_528_Closed_Image" style="display: none" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" border="0" align="top" /> &nbsp; &nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(var i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">10</span><span style="color: #000000">;i</span><span style="color: #000000">++</span><span style="color: #000000"> )</span><span style="border: 1px solid #808080; display: none; background-color: #ffffff">...</span><span style="display: inline"><span style="color: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;myArray[i]</span><span style="color: #000000">=</span><span style="color: #0000ff">new</span><span style="color: #000000"> Array();<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;myArray[i][</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">Math.floor(Math.random()</span><span style="color: #000000">*</span><span style="color: #000000">10</span><span style="color: #000000">); <img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;myArray[i][</span><span style="color: #000000">1</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">Math.floor(Math.random()</span><span style="color: #000000">*</span><span style="color: #000000">10</span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;myArray[i][</span><span style="color: #000000">2</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">Math.floor(Math.random()</span><span style="color: #000000">*</span><span style="color: #000000">10</span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;myArray[i][</span><span style="color: #000000">3</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">Math.floor(Math.random()</span><span style="color: #000000">*</span><span style="color: #000000">10</span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;myArray[i][</span><span style="color: #000000">4</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">Math.floor(Math.random()</span><span style="color: #000000">*</span><span style="color: #000000">10</span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;myArray[i][</span><span style="color: #000000">5</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">Math.floor(Math.random()</span><span style="color: #000000">*</span><span style="color: #000000">10</span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;myArray[i][</span><span style="color: #000000">6</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">Math.floor(Math.random()</span><span style="color: #000000">*</span><span style="color: #000000">10</span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;myArray[i][</span><span style="color: #000000">7</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">Math.floor(Math.random()</span><span style="color: #000000">*</span><span style="color: #000000">10</span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;myArray[i][</span><span style="color: #000000">8</span><span style="color: #000000">]</span><span style="color: #000000">=</span><span style="color: #000000">Math.floor(Math.random()</span><span style="color: #000000">*</span><span style="color: #000000">10</span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" border="0" align="top" /> &nbsp; &nbsp;}</span></span><span style="color: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" border="0" align="top" /> &nbsp; &nbsp;<br /><img id="_562_638_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" border="0" align="top" /><img id="_562_638_Closed_Image" style="display: none" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" border="0" align="top" /> &nbsp; &nbsp;myArray.sort( function(x, y) </span><span style="border: 1px solid #808080; display: none; background-color: #ffffff">...</span><span><span style="color: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000"> (x[</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">==</span><span style="color: #000000">y[</span><span style="color: #000000">0</span><span style="color: #000000">])</span><span style="color: #000000">?</span><span style="color: #000000">((x[</span><span style="color: #000000">4</span><span style="color: #000000">]</span><span style="color: #000000">==</span><span style="color: #000000">y[</span><span style="color: #000000">4</span><span style="color: #000000">])</span><span style="color: #000000">?</span><span style="color: #000000">(x[</span><span style="color: #000000">8</span><span style="color: #000000">]</span><span style="color: #000000">-</span><span style="color: #000000">y[</span><span style="color: #000000">8</span><span style="color: #000000">]):(x[</span><span style="color: #000000">4</span><span style="color: #000000">]</span><span style="color: #000000">-</span><span style="color: #000000">y[</span><span style="color: #000000">4</span><span style="color: #000000">])):(x[</span><span style="color: #000000">2</span><span style="color: #000000">]</span><span style="color: #000000">-</span><span style="color: #000000">y[</span><span style="color: #000000">2</span><span style="color: #000000">])<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" border="0" align="top" /> &nbsp; &nbsp;}</span></span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" border="0" align="top" /><br /><img id="_678_731_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" border="0" align="top" /><img id="_678_731_Closed_Image" style="display: none" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" border="0" align="top" /> &nbsp; &nbsp;</span><span style="color: #0000ff">for</span><span style="color: #000000">(var i</span><span style="color: #000000">=</span><span style="color: #000000">0</span><span style="color: #000000">;i</span><span style="color: #000000">&lt;</span><span style="color: #000000">myArray.length;i</span><span style="color: #000000">++</span><span style="color: #000000"> )</span><span style="border: 1px solid #808080; display: none; background-color: #ffffff">...</span><span><span style="color: #000000">{<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/InBlock.gif" border="0" align="top" /> &nbsp; &nbsp; &nbsp; &nbsp;document.write(myArray[i].join(</span><span style="color: #000000">&quot;</span><span style="color: #000000">,</span><span style="color: #000000">&quot;</span><span style="color: #000000">) </span><span style="color: #000000">+</span><span style="color: #000000"> </span><span style="color: #000000">&quot;</span><span style="color: #000000">&lt;br/&gt;</span><span style="color: #000000">&quot;</span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" border="0" align="top" /> &nbsp; &nbsp;}</span></span><span style="color: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" border="0" align="top" /></span><span style="color: #000000">&lt;/</span><span style="color: #000000">script</span><span style="color: #000000">&gt;</span><span style="color: #000000"><br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/None.gif" border="0" align="top" /></span></div></div><p><strong> 字符：</strong></p><p>字符的时候sortFunction中的项目不能像数字一样直接相减，需要调用<strong><em>str1.localeCompare( str2 )</em></strong>方法来作比较，从而满足返回值。以下是多维数组的第1，2列作排序的情况。</p><div style="background: #e6e6e6; padding: 4px 5.4pt; border: 0.5pt solid windowtext; width: 95%; word-break: break-all"><div><img id="_30_145_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockStart.gif" border="0" align="top" /><img id="_30_145_Closed_Image" style="display: none" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedBlock.gif" border="0" align="top" /><span style="color: #0000ff">function</span><span style="color: #000000"> sortFunction(array) </span><span style="border: 1px solid #808080; display: none; background-color: #ffffff">...</span><span><span style="color: #000000">{<br /><img id="_67_141_Open_Image" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedSubBlockStart.gif" border="0" align="top" /><img id="_67_141_Closed_Image" style="display: none" src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ContractedSubBlock.gif" border="0" align="top" /> &nbsp; &nbsp;</span><span style="color: #0000ff">return</span><span style="color: #000000"> array.sort( </span><span style="color: #0000ff">function</span><span style="color: #000000">(x, y) </span><span style="border: 1px solid #808080; display: none; background-color: #ffffff">...</span><span><span style="color: #000000">{</span><span style="color: #0000ff">return</span><span style="color: #000000"> (x[</span><span style="color: #000000">0</span><span style="color: #000000">]</span><span style="color: #000000">==</span><span style="color: #000000">y[</span><span style="color: #000000">0</span><span style="color: #000000">])</span><span style="color: #000000">?</span><span style="color: #000000">(x[</span><span style="color: #000000">1</span><span style="color: #000000">].localeCompare(y[</span><span style="color: #000000">1</span><span style="color: #000000">])):(x[</span><span style="color: #000000">0</span><span style="color: #000000">].localeCompare(y[</span><span style="color: #000000">0</span><span style="color: #000000">]))}</span></span><span style="color: #000000">);<br /><img src="http://images.csdn.net/syntaxhighlighting/OutliningIndicators/ExpandedBlockEnd.gif" border="0" align="top" />}</span></span></div></div><p> 因此<strong>arrayObject.sort( sortFunction )</strong>的排序功能还是很强大的，终于能够实现了SQL语句中的ORDER BY 一样的功能。</p><br/>Tags - <a href="tag.php?tag=javascript" rel="tag">javascript</a>
]]>
</description>
</item><item>
<link>http://www.bizeway.net/read.php?&amp;guid=0#topreply</link>
<title><![CDATA[[评论] JavaScript：多维数组像ORDER BY一样实现多键值排序 ]]></title> 
<author> &lt;user@domain.com&gt;</author>
<category><![CDATA[评论]]></category>
<pubDate>Thu, 01 Jan 1970 00:00:00 +0000</pubDate> 
<guid>http://www.bizeway.net/read.php?&amp;guid=0#topreply</guid> 
<description>
<![CDATA[ 
	
]]>
</description>
</item>
</channel>
</rss>