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. Sumdiv POJ 1845

    http://poj.org/problem?id=1845 题目 Time Limit: 1000MS   Memory Limit: 30000K Description Consider two ...

  2. placeholder效果

    <!DOCTYPE HTML> <html lang="en-US"> <head>     <meta charset="UT ...

  3. Nginx-Tomcat搭建负载均衡(转载)

    一.   工具 nginx-1.8.0 apache-tomcat-6.0.33 二.    目标 实现高性能负载均衡的Tomcat集群: 三.    步骤 1.首先下载Nginx,要下载稳定版: 2 ...

  4. 满汉全席[2-SAT]

    题面 对不起我又写了一个板题qvq 和洛谷那道模板题没区别...两样菜至少做一样即可 不过注意define和函数的区别!!! #include <cmath> #include <c ...

  5. R语言统计学习-1简介

    一. 统计学习概述 统计学习是指一组用于理解数据和建模的工具集.这些工具可分为有监督或无监督.1.监督学习:用于根据一个或多个输入预测或估计输出.常用于商业.医学.天体物理学和公共政策等领域.2.无监 ...

  6. 网页三剑客之HTML

    Web服务本质 import socket def handle_request(client): request_data = client.recv(1024) print(request_dat ...

  7. [物理学与PDEs]第1章习题7 载流线圈的磁场

    设一半径为 $R$ 的圆周电路上的电流强度为 $I$. 试计算在通过圆心垂直于圆周所在平面的直线上, 由该圆周电路产生的磁场的磁感强度. 解答: 由对称性知在该直线 $l$ 上, ${\bf B}$ ...

  8. Coursera, Big Data 3, Integration and Processing (week 1/2/3)

    This is the 3rd course in big data specification courses. Data model reivew 1, data model 的特点: Struc ...

  9. luogu P5289 [十二省联考2019]皮配

    传送门 首先考虑一个正常的dp,设\(f_{i,j,k}\)为前\(i\)个学校,\(j\)人在\(\color{#0000FF}{蓝阵营}\),\(k\)人在\(\color{#654321}{吔} ...

  10. 第29月第21天 ios android curl

    1.curl https://github.com/gcesarmza/curl-android-ios/ 2.json https://github.com/danielmapar/cpp-scra ...