如何使用 Go 对 csv 文件进行字母排序?

我正在尝试使用 Go 根据姓氏(位于第一列)中按字母顺序排列具有一系列名称的 .csv 文件。我已经搜索了所有内容,但我似乎无法找到一种方法来做到这一点。有没有办法做到这一点,同时保持同一行中的其他值?我有三个同名的 .csv 文件,但我必须对它们进行洗牌才能完成我的任务(随机餐桌座位算法)。我希望能够将它们重新按定义的字母顺序排列,这样我就可以确定人们不会连续坐在一起。


提前致谢。:)


编辑:可能值得展示我用来洗牌的功能:


func Shuffle(slice []Person) []Person {

r := rand.New(rand.NewSource(time.Now().Unix()))

ret := make([]Person, len(slice))

n := len(slice)

for i := 0; i < n; i++ {

    randIndex := r.Intn(len(slice))

    ret[i] = slice[randIndex]

    slice = append(slice[:randIndex], slice[randIndex+1:]...)

}

return ret

Person[] 切片只是一个包含名字和姓氏的结构。


宝慕林4294392
浏览 139回答 1
1回答

肥皂起泡泡

Go 的sort包附带了一个很好的例子。请参阅修改后的实现,该实现应该可以满足您的要求。package mainimport (&nbsp; &nbsp; "encoding/csv"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "io"&nbsp; &nbsp; "log"&nbsp; &nbsp; "sort"&nbsp; &nbsp; "strings")// Unsorted sample datavar unsorted = `Balaam,Wileen,Saint LouisMeachan,Lothaire,LefengzhenScoggin,Ivonne,PagHawarden,Audrye,LeiriaClaypool,Biddy,MaiorcaStanford,Douglas,BáguanosPetriello,Yvor,ObryteHatter,Margette,LuopingPepall,Linzy,HucunCarter,Kit,Parungjawa`type Person struct {&nbsp; &nbsp; Lastname&nbsp; string&nbsp; &nbsp; Firstname string&nbsp; &nbsp; City&nbsp; &nbsp; &nbsp; string}// Create a new Person record from a given string slicefunc NewPerson(fields []string) (p Person, err error) {&nbsp; &nbsp; if len(fields) < 3 {&nbsp; &nbsp; &nbsp; &nbsp; return p, fmt.Errorf("not enough data for Person")&nbsp; &nbsp; }&nbsp; &nbsp; p.Lastname = fields[0]&nbsp; &nbsp; p.Firstname = fields[1]&nbsp; &nbsp; p.City = fields[2]&nbsp; &nbsp; return}// ByLastname implements sort.Interface for []Person based on the Lastname field.type ByLastname []Personfunc (a ByLastname) Len() int&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{ return len(a) }func (a ByLastname) Swap(i, j int)&nbsp; &nbsp; &nbsp; { a[i], a[j] = a[j], a[i] }func (a ByLastname) Less(i, j int) bool { return a[i].Lastname < a[j].Lastname }func main() {&nbsp; &nbsp; // Open unsorted CSV from string&nbsp; &nbsp; r := csv.NewReader(strings.NewReader(unsorted))&nbsp; &nbsp; var people []Person&nbsp; &nbsp; for {&nbsp; &nbsp; &nbsp; &nbsp; // Read CSV line by line&nbsp; &nbsp; &nbsp; &nbsp; record, err := r.Read()&nbsp; &nbsp; &nbsp; &nbsp; if err == io.EOF {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.Fatal(err)&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; // Create Person from line in CSV&nbsp; &nbsp; &nbsp; &nbsp; person, err := NewPerson(record)&nbsp; &nbsp; &nbsp; &nbsp; if err != nil {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; continue&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; people = append(people, person)&nbsp; &nbsp; }&nbsp; &nbsp; // Sort CSV by Lastname&nbsp; &nbsp; sort.Sort(ByLastname(people))&nbsp; &nbsp; // Print to stdout&nbsp; &nbsp; for _, p := range people {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Printf("%s %s from %s\n", p.Lastname, p.Firstname, p.City)&nbsp; &nbsp; }&nbsp; &nbsp; // Here you would write your CSV}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go