#Sort

25 messages · Page 1 of 1 (latest)

mental crescent
#

I was sorting two structs this way, before

type TwoSlices struct {
    main_slice       []float64
    other_slice       []string
}

type SortByOther TwoSlices

func (sbo SortByOther) Len() int {
    return len(sbo.main_slice)
}

func (sbo SortByOther) Swap(i, j int) {
    sbo.main_slice[i], sbo.main_slice[j] = sbo.main_slice[j], sbo.main_slice[i]
    sbo.other_slice[i], sbo.other_slice[j] = sbo.other_slice[j], sbo.other_slice[i]
}

func (sbo SortByOther) Less(i, j int) bool {
    return sbo.other_slice[i] < sbo.other_slice[j] 
}

var Leaderboard = make([]string, 1)
var Leaderboard_WPM = make([]float64, 1)

Leaderboard = ...
Leaderboard_WPM = ...

var my_two_slices = TwoSlices{main_slice: Leaderboard_WPM, other_slice: Leaderboard}

sort.Sort(SortByOther(my_two_slices))```

at first glance, it worked, but now it failed to sort and just kept everything the same
spice escarp
#

Hrm... This ought to work, as long as the slices have the same length and start out paired up correctly.

#

Maybe check those preconditions. They're not clear from what you posted.

mental crescent
#

one slice is [0 101.2 202.2 159.3]
I don't know where that 0 is coming from (honestly), is that game changing? ...

spice escarp
#

If the two slices have different lengths it could be disasterous.

#

If you don't know why that zero is there it could be a clue.

#

Your bug may not be in the sorting. This seems sane to me (given those preconditions).

spice escarp
#

You probably appended to the slice after using make([]int, 1)

#

That will allocate a slice of length one which is filled with zeroes.

#

If you're intending to allocate a length zero slice with capacity one the syntax is make([]int, 0, 1).

#

I think that's where your extra zero is coming from.

mental crescent
#

it sorts
[0 101.2 202.2 159.3]
into
[0 159.3 101.2 202.2]

#

with make([]int, 0, 1) it doesn't sort

#

maybe the sort just doesn't work, right?

solemn hare
#

could you share any more info about the slices and how they're appended to?

solemn hare
mental crescent
#

WPM_f64 is

WPM_f64, _ := strconv.ParseFloat(CL[3], 8)
#
    for i := 0; i < len(DB); i++ {
        if strings.HasPrefix(DB[i], WHERE_str + " #") {
            if i == 0 {
                var CL = strings.Split(DB[i], " # ")
                WPM_f64, _ := strconv.ParseFloat(CL[3], 8)
                Leaderboard_WPM[0] = WPM_f64
                Leaderboard[0] = (CL[2] + " (" + CL[3] + " WPM) " + CL[4])
            } else {
                var CL = strings.Split(DB[i], " # ")
                WPM_f64, _ := strconv.ParseFloat(CL[3], 8)
                Leaderboard_WPM = append(Leaderboard_WPM, WPM_f64)
                Leaderboard = append(Leaderboard, (CL[2] + " (" + CL[3] + " WPM) " + CL[4]))
            }

            FOUND = true
            FOUND_how_many_times++
        }
    }```
craggy prairie
#

func (sbo SortByOther) Swap(i, j int) {
When this is called, it works on a copy of sbo.
Did you want func (sbo *SortByOther) Swap(i, j int) {?

mental crescent
# craggy prairie `func (sbo SortByOther) Swap(i, j int) {` When this is called, it works on a *co...
source/leaderboards.go:73:15: cannot use SortByOther(my_two_slices) (value of type SortByOther) as type sort.Interface in argument to sort.Sort:
    SortByOther does not implement sort.Interface (Swap method has pointer receiver)
source/leaderboards.go:137:15: cannot use SortByOther(my_two_slices) (value of type SortByOther) as type sort.Interface in argument to sort.Sort:
    SortByOther does not implement sort.Interface (Swap method has pointer receiver)```
spice escarp
#

Make them all have pointer receivers.

#

Wait. That shouldn't matter here, slices are reference types, right?

mental crescent
#

Yes