d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Is There A Better Way? > To Sort A Map[string]int By Value?
123Next
Add Reply New Topic New Poll
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 12 2016 04:53pm
i feel like theres gotta be a better way that doesnt go to n^2 i just can't think of it

Code
import (
"fmt"
"sort"
)

func main() {
nameList := make (map[string]int)

nameList["Joe"]=48
nameList["Karen"]=17
nameList["Abash"]=17
nameList["Drake"]=1
nameList["Will"]=23

for index, value := range nameList{
fmt.Printf("%v %v \n", index, value)
}
var indexes []string
for index:= range nameList{
indexes= append(indexes, index)
}

sort.Strings(indexes)
fmt.Println("===========Sortedbykey=======")
for _, name := range indexes {
fmt.Printf("%v %v\n", name, nameList[name])
}
var values []int
for i := range indexes {
values=append(values, nameList[indexes[i]])
}
sort.Ints(values)
fmt.Println("===========Sortedbyvalue=======")

for _,i := range values {
for j, _ := range nameList{
if i== nameList[j]{
fmt.Printf("%v %v\n", j, nameList[j])
i=-1
}
}
}
}


e. with no base functions besides a sort that cannot be used on the maps

This post was edited by Ideophobe on Apr 12 2016 04:57pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 12 2016 05:51pm
/edit: from googling golang sort map by values:
http://stackoverflow.com/questions/18695346/how-to-sort-a-mapstringint-by-its-values

This post was edited by carteblanche on Apr 12 2016 06:06pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 12 2016 06:08pm
Does golang really not provide sort_by methods on their hashes...?
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 12 2016 06:25pm
Quote (carteblanche @ Apr 12 2016 05:51pm)


ya i saw that 2 responses, 2nd guy did about the same thing i did, first guy made pair structs and wrote a nasty sort which is definately better than n^2 but still flirting with it
i feel like there should be a way to get down to n

Quote (AbDuCt @ Apr 12 2016 06:08pm)
Does golang really not provide sort_by methods on their hashes...?


omg u gave me a great idea i can just reverse them... fuck lets see if i can figure out how to do this

nvm thats not gonna work..

This post was edited by Ideophobe on Apr 12 2016 06:29pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 12 2016 07:05pm
nvm that doesnt work either lol

This post was edited by Ideophobe on Apr 12 2016 07:12pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 12 2016 07:14pm
Quote (Ideophobe @ Apr 12 2016 08:25pm)
ya i saw that 2 responses, 2nd guy did about the same thing i did, first guy made pair structs and wrote a nasty sort which is definately better than n^2 but still flirting with it

"nasty" sort? he just implemented the interfaces for the built in sort. that looks like the correct decision. i mean, you can just create a second map with the values / keys inverted and sort that by key if you're happy with that. or similarly, an array of structs which you sort by value.

Quote
i feel like there should be a way to get down to n


you can't do a generic comparison sort in O(n). it would have to be something that exploits the integers or some other property you haven't mentioned. you can do a radix sort or something if you want. if you know there's a small domain of integers, you can do a bitvector sort. or whatever your favourite integer sort is. but IMO you should stick with the built in sort. it's probably optimized for performance using native code.

This post was edited by carteblanche on Apr 12 2016 07:30pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 12 2016 07:50pm
Quote (carteblanche @ Apr 12 2016 07:14pm)
"nasty" sort? he just implemented the interfaces for the built in sort. that looks like the correct decision. i mean, you can just create a second map with the values / keys inverted and sort that by key if you're happy with that. or similarly, an array of structs which you sort by value.



you can't do a generic comparison sort in O(n). it would have to be something that exploits the integers or some other property you haven't mentioned. you can do a radix sort or something if you want. if you know there's a small domain of integers, you can do a bitvector sort. or whatever your favourite integer sort is. but IMO you should stick with the built in sort. it's probably optimized for performance using native code.

i didn't look at his whole code before..... you could overload a comparable on an object too, that's probably the best way to go

what a waste of time this was... sometimes i really need to just take a step back and get myself out of the tunnel
Code
var values []int
for i := range indexes {
values=append(values, nameList[indexes[i]])
}
sort.Ints(values)
fmt.Println("===========Sortedbyvalue=======")


for _,i := range values {
Loop: for j:= range nameList{
if i== nameList[j]{
fmt.Printf("%v %v\n", j, nameList[j])
delete(nameList, j)
break Loop
}
}
}

nasty, i could do a binary search on it and get even closer but... fuck it now that i know no matter what i do it wont be as good as going to object i'm totally bummed

anyway thanks carte

This post was edited by Ideophobe on Apr 12 2016 08:13pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 12 2016 08:32pm
Is golang really that stupid that you need to implement your own sorts? You'd think that they would have their own Enumerable sort already implemented which is optimized for all data types.

Code
irb(main):006:0> nameList = {"Joe" => 48, "Karen" => 17, "Abash" => 17, "Drake>
=> {"Joe"=>48, "Karen"=>17, "Abash"=>17, "Drake"=>1, "Will"=>23}
irb(main):003:0> nameList.sort_by { |k,v| k }
=> [["Abash", 17], ["Drake", 1], ["Joe", 48], ["Karen", 17], ["Will", 23]]
irb(main):004:0> nameList.sort_by { |k,v| v }
=> [["Drake", 1], ["Karen", 17], ["Abash", 17], ["Will", 23], ["Joe", 48]]


This post was edited by AbDuCt on Apr 12 2016 08:34pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 12 2016 08:59pm
Quote (AbDuCt @ Apr 12 2016 10:32pm)
Is golang really that stupid that you need to implement your own sorts? You'd think that they would have their own Enumerable sort already implemented which is optimized for all data types.

Code
irb(main):006:0> nameList = {"Joe" => 48, "Karen" => 17, "Abash" => 17, "Drake>
=> {"Joe"=>48, "Karen"=>17, "Abash"=>17, "Drake"=>1, "Will"=>23}
irb(main):003:0> nameList.sort_by { |k,v| k }
=> [["Abash", 17], ["Drake", 1], ["Joe", 48], ["Karen", 17], ["Will", 23]]
irb(main):004:0> nameList.sort_by { |k,v| v }
=> [["Drake", 1], ["Karen", 17], ["Abash", 17], ["Will", 23], ["Joe", 48]]


as long as someone can write a library to implement it, i don't care much if it's already part of the standard library.
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 12 2016 10:20pm
i havent seen any language that has a method to sort hashmaps directly, but as carte pointed out earlier, that's why god invented oop right?
just having fun is all in any case it will always be easy enough to convert a map into a linked list

but ya golang has the same quicksort everyone else does it just doesnt have an overload for maps
https://golang.org/src/sort/sort.go

quick sort is a super interesting algorithm



This post was edited by Ideophobe on Apr 12 2016 10:25pm
Go Back To Programming & Development Topic List
123Next
Add Reply New Topic New Poll