<strong>1 提供less(i, j int) bool函数</strong>
<br />
<strong>1.1 golang代码</strong>
<br />
<div class="dp-highlighter"><div class="bar"></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">package</span><span> main </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">import</span><span> ( </span></span></li><li class=""><span> <span class="string">"fmt"</span><span> </span></span></li><li class="alt"><span> <span class="string">"sort"</span><span> </span></span></li><li class=""><span>) </span></li><li class="alt"><span> </span></li><li class=""><span>func main() { </span></li><li class="alt"><span> ps := []struct { </span></li><li class=""><span> name string </span></li><li class="alt"><span> age <span class="keyword">int</span><span> </span></span></li><li class=""><span> }{ </span></li><li class="alt"><span> {<span class="string">"larry"</span><span>, </span><span class="number">19</span><span>}, </span></span></li><li class=""><span> {<span class="string">"jackey"</span><span>, </span><span class="number">18</span><span>}, </span></span></li><li class="alt"><span> {<span class="string">"lucy"</span><span>, </span><span class="number">20</span><span>}, </span></span></li><li class=""><span> } </span></li><li class="alt"><span> <span class="comment">// keeping the original order of equal elements</span><span> </span></span></li><li class=""><span> sort.SliceStable(ps, func(i, j <span class="keyword">int</span><span>) bool { </span></span></li><li class="alt"><span> <span class="keyword">return</span><span> ps[i].age < ps[j].age </span></span></li><li class=""><span> }) </span></li><li class="alt"><span> fmt.Println(ps) </span></li><li class=""><span>} </span></li></ol></div>
<br />
<strong>1.2 结果输出</strong>
<br />
<div class="dp-highlighter nogutter"><div class="bar"></div><ol start="0" class="dp-j"><li class="alt"><span><span>[{jackey </span><span class="number">18</span><span>} {larry </span><span class="number">19</span><span>} {lucy </span><span class="number">20</span><span>}] </span></span></li></ol></div>
<br />
<strong>2 实现sort.Interface接口</strong>
<br />
<strong>2.1 golang代码</strong>
<br />
<div class="dp-highlighter"><div class="bar"></div><ol start="1" class="dp-j"><li class="alt"><span><span class="keyword">package</span><span> main </span></span></li><li class=""><span> </span></li><li class="alt"><span><span class="keyword">import</span><span> ( </span></span></li><li class=""><span> <span class="string">"fmt"</span><span> </span></span></li><li class="alt"><span> <span class="string">"sort"</span><span> </span></span></li><li class=""><span>) </span></li><li class="alt"><span> </span></li><li class=""><span>type person struct { </span></li><li class="alt"><span> name string </span></li><li class=""><span> age <span class="keyword">int</span><span> </span></span></li><li class="alt"><span>} </span></li><li class=""><span> </span></li><li class="alt"><span>type persons []person </span></li><li class=""><span> </span></li><li class="alt"><span>func (ps persons) Len() <span class="keyword">int</span><span> { </span></span></li><li class=""><span> <span class="keyword">return</span><span> len(ps) </span></span></li><li class="alt"><span>} </span></li><li class=""><span> </span></li><li class="alt"><span>func (ps persons) Less(i, j <span class="keyword">int</span><span>) bool { </span></span></li><li class=""><span> <span class="keyword">return</span><span> ps[i].age < ps[j].age </span></span></li><li class="alt"><span>} </span></li><li class=""><span> </span></li><li class="alt"><span>func (ps persons) Swap(i, j <span class="keyword">int</span><span>) { </span></span></li><li class=""><span> ps[i], ps[j] = ps[j], ps[i] </span></li><li class="alt"><span>} </span></li><li class=""><span> </span></li><li class="alt"><span>func main() { </span></li><li class=""><span> ps := persons{ </span></li><li class="alt"><span> {<span class="string">"larry"</span><span>, </span><span class="number">19</span><span>}, </span></span></li><li class=""><span> {<span class="string">"jackey"</span><span>, </span><span class="number">18</span><span>}, </span></span></li><li class="alt"><span> {<span class="string">"lucy"</span><span>, </span><span class="number">20</span><span>}, </span></span></li><li class=""><span> } </span></li><li class="alt"><span> sort.Sort(ps) </span></li><li class=""><span> fmt.Println(ps) </span></li><li class="alt"><span>} </span></li></ol></div>
<br />
<strong>2.2 结果输出</strong>
<br />
<div class="dp-highlighter nogutter"><div class="bar"></div><ol start="0" class="dp-j"><li class="alt"><span><span>[{jackey </span><span class="number">18</span><span>} {larry </span><span class="number">19</span><span>} {lucy </span><span class="number">20</span><span>}] </span></span></li></ol></div>
<br />
原文地址:https://leileiluoluo.com/posts/golang-struct-slice-sorting.html
有疑问加站长微信联系(非本文作者))