sort.go
author Mikael Berthe <mikael@lilotux.net>
Sat, 13 Oct 2018 21:42:11 +0200
changeset 34 b70346ff153d
permissions -rw-r--r--
Split goduf.go into several files
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
34
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     1
/*
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     2
 * Copyright (C) 2014-2018 Mikael Berthe <mikael@lilotux.net>
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     3
 *
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     4
 * This program is free software; you can redistribute it and/or modify
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     5
 * it under the terms of the GNU General Public License as published by
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     6
 * the Free Software Foundation; either version 2 of the License, or (at
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     7
 * your option) any later version.
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
 *
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
 * This program is distributed in the hope that it will be useful, but
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
 * WITHOUT ANY WARRANTY; without even the implied warranty of
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
 * General Public License for more details.
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
 *
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
 * You should have received a copy of the GNU General Public License
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
 * along with this program; if not, write to the Free Software
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
 * USA
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
 */
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
package main
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
// Implement a sort interface for the list of duplicate groups
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
type byGroupFileSize foListList
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
func (a byGroupFileSize) Len() int      { return len(a) }
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
func (a byGroupFileSize) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
func (a byGroupFileSize) Less(i, j int) bool {
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	// Since this is supposed to be used for duplicate lists,
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
	// we use the size of the first file of the group.
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	if a[i][0].Size() == a[j][0].Size() {
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
		return a[i][0].FilePath < a[j][0].FilePath
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	}
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	return a[i][0].Size() < a[j][0].Size()
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
}
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    36
// Implement a sort interface for a slice of files
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
type byFilePathName FileObjList
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
func (a byFilePathName) Len() int      { return len(a) }
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
func (a byFilePathName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    41
func (a byFilePathName) Less(i, j int) bool {
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
	return a[i].FilePath < a[j].FilePath
b70346ff153d Split goduf.go into several files
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
}