package main

import (
"fmt"
"sort"
"time"
) type Track struct {
Title string
Artist string
Album string
Year int
Length time.Duration
} type Tracks []Track func ParseDurationTime(s string) time.Duration {
d, err := time.ParseDuration(s)
if err != nil {
return ParseDurationTime("0s")
} else {
return d
}
} // {{{ implementation of Sort interface
// Len is the number of elements in the collection.
func (x Tracks) Len() int {
return len(x)
} // Less reports whether the element with
// index i should sort before the element with index j.
func (x Tracks) Less(i, j int) bool {
return x[i].Year < x[j].Year
} // Swap swaps the elements with indexes i and j.
func (x Tracks) Swap(i, j int) {
x[i], x[j] = x[j], x[i]
} // end implementation of Sort interface }}} var tracks = Tracks{
{Title: "C#", Artist: "Delu", Album: "Reading", Year: 2017, Length: ParseDurationTime("3m38s")},
{Title: "Go", Artist: "Anderson", Album: "Reading", Year: 2018, Length: ParseDurationTime("3m38s")}, {Title: "Java Bible", Artist: "Js", Album: "Reading", Year: 2016, Length: ParseDurationTime("3m38s")}} //main function
func main() { sort.Sort(tracks) for key, value := range tracks {
fmt.Printf("%v:%v \n", key, value)
}
}

  

Go的sort接口实现的更多相关文章

  1. autofac 一个接口多个实现的顺序执行

    接口: namespace AutofacTest.Interface { public interface IUserInfo { string GetUserINfo(int uid); int ...

  2. Go 语言接口及使用接口实现链表插入

    @ 目录 1. 接口定义 1.1 空接口 1.2 实现单一接口 1.3 接口多方法实现 2. 多态 2.1 为不同数据类型的实体提供统一的接口 2.2 多接口的实现 3. 系统接口调用 4. 接口嵌套 ...

  3. Go语言实战 - 我需要站内搜索

    山坡网的用户抱怨"为什么搜索'二鬼子李富贵'找不到'二鬼子汉奸李富贵'?我用百度搜都能找到." 当时我就滴汗了,用户说的有道理,应该要能搜索到. 之前的方案很简单,用户输入的字串会 ...

  4. Java中HashMap排序

    注: 转载于 http://www.cnblogs.com/xingyun/archive/2012/12/09/2809962.html package com.holdobject; import ...

  5. 汇编与C语言混合 实现的从小到大的冒泡排序

    汇编实现的从小到大的冒泡排序 主函数由C语言实现,sort函数用汇编语言写 #include <stdio.h>  int buffer[256];      //数据缓冲区  int   ...

  6. Effective Go -> Interface

    1.接口实现及类型转换 type Sequence []int // Methods required by sort.Interface. func (s Sequence) Len() int { ...

  7. consistent.go 源码阅读

    ) > len(c.circle) {         hashes = nil     }     for k := range c.circle {         hashes = app ...

  8. Python基础(9) - 类

    Python 看下面一个简单类: >>> class MyClass(object): ... """ ... this is a class with ...

  9. 从头认识java-14.4 Java提供的数组的有用功能(2)

    接着上一章节,我们继续介绍Java提供的数组的有用功能. 3.元素的对照Comparator package com.ray.ch14; import java.util.Arrays; import ...

随机推荐

  1. 四、Java多人博客系统-2.0版本

    由于时间关系,多人博客系统这里穿插一个2.0版本. 2.0版本本来是打算用于建立个人网站,但是后来发现个人建站需要购买域名服务器,还需要备案,很繁琐.最终放弃.完成此版本,最终也只是作为技术演练.此版 ...

  2. socket之黏包

    一.黏包成因 1.tcp协议的拆包机制 当发送端缓冲区的长度大于网卡的MTU时,tcp会将这次发送的数据拆成几个数据包发送出去. MTU是Maximum Transmission Unit的缩写.意思 ...

  3. shell之数学运算

    let #!/bin/bash no1=1; no2=5; let result=no1+no2 ##不能留空格 echo $result #自加 let no++ #自减 let no-- #简写 ...

  4. Vue(小案例_vue+axios仿手机app)_上拉加载

    ---恢复内容开始--- 一.前言                                                                                    ...

  5. CMDB服务器管理系统【s5day91】:资产采集相关问题

    资产采集唯一标识和允许临时修改主机名 class AgentClient(BaseClient): def exec(self): obj = PluginManager() server_dict ...

  6. 金融量化分析【day111】:Matplotib-画布与子图

    一.画布与子图 1.实例 %matplotlib auto fig = plt.figure() ax = fig.add_subplot(2,2,1) ax2 = fig.add_subplot(2 ...

  7. Nuxt.js学习

    SSR服务端渲染 之前用vue做项目时,在浏览器中查看网页源码,是没有具体内容的,只有一个标签,用服务端渲染的话,查看网页源码数据都会显示出来,所以有利于SEO,能够被搜索到. Nuxt.js是做Vu ...

  8. beanPostProcessor与beanFactoryPostProcessor

    BeanFactoryPostProcessor的典型应用:PropertyPlaceholderConfigurer BeanFactoryPostProcessor会在所有的bean配置载入之后执 ...

  9. DTO/DO等POJO对象的使用场景和 orika-mapper 框架的使用

    对于项目而言, 我们一般会有DAO->Service->Controller分层设计, 这些层次体现了每层的作用, 而层次之间的数据传递对象设计很少被提及, 下面是一个相对完整的数据转换过 ...

  10. 获取reporting services导出pdf的url的方法

    public static string genRptUrl(string strRptServer, string strRptPath, string strRptName, ParameterV ...