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. javaWeb1之Servlet

    Servlet Servlet 环境设置 servlet是扩展web服务器功能的组件规范.浏览器发送请求给web服务器,如果是动态资源的请求,web服务器会将请求转发给servlet容器来处理(由容器 ...

  2. BZOJ4671异或图

    题目描述 定义两个结点数相同的图 G1 与图 G2 的异或为一个新的图 G, 其中如果 (u, v) 在 G1 与 G2 中的出现次数之和为 1, 那么边 (u, v) 在 G 中, 否则这条边不在 ...

  3. CF226D The table

    题目链接 题意 给出一个\(n\times m\)的矩阵,可以把某些行和某些列上面的数字变为相反数.问修改那些行和哪些列可以使得所有行和所有列之和都为非负数. 思路 每次将负数的行或者列变为相反数.因 ...

  4. jpg、png格式的图片转换成webp后颜色失真的问题

    今天简单的试用了一下 cweb.exe 将 jpg, png 格式的图片转换成 webp 格式. 我今天下载的是当前最新版:1.0.0 cwebp 3.jpg  -q 85 -o 3.webp 发现图 ...

  5. SP687 REPEATS - Repeats

    给定字符串,求重复次数最多的连续重复子串. 题目很简单,被细节坑惨了... 前置的一个推论:请看这里. #include <bits/stdc++.h> using namespace s ...

  6. Luogu P3731 [HAOI2017]新型城市化

    题目显然可以转化为求每一条边对二分图最大独立集的贡献,二分图最大独立集\(=\)点数\(-\)最大匹配数,我们就有了\(50pts\)做法. 正解的做法是在原图上跑\(Tarjan\),最开始我想复杂 ...

  7. mysql快速生成批量测试数据

    mysql快速生成批量测试数据 参考资料: https://blog.csdn.net/oahz4699092zhao/article/details/53332148 -- 创建一个临时内存表 ; ...

  8. prometheus 配置介绍

    prometheus 配置介绍 prometheus 配置分global.alerting.rule_files.scrape_configs 1.global(全局配置) scrape_interv ...

  9. Class.forname和ClassLoader.loadClass的源码分析

    最近在研读<深入理解java虚拟机:JVM高继特性与最佳实践>第二版, 今天想起来很久前,写数据库连接,使用Class.forName,当时没有深究,所以便简单的看了下源码,顺便做了以下记 ...

  10. C#处理非托管资源

    using System; //处理非托管资源 //例如:文件句柄.网络连接.数据库连接 //实现IDisposable不意味着也应该实现一个终结器,终结器会带来额外开销 //发布本机资源,要释放本机 ...