d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Is There A Better Way? > To Sort A Map[string]int By Value?
Prev123Next
Add Reply New Topic New Poll
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 12 2016 10:32pm
Quote (Ideophobe @ Apr 13 2016 12:20am)
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

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


As far as I know it is not possible to sort hashes directly since as far as I know they are just linked lists with the keys hashed to predict the index into the list, or just a unique identifier to find the correct value.

Although if you wanted to implement this yourself create an array of arrays then apply your sort on either the first index (key) or the second index (value). I believe this is what most languages do anyways with their built in sorts:

Code
irb(main):003:0> nameList = {"Joe"=>48, "Karen"=>17, "Abash"=>17, "Drake"=>1, "Will"=>23}
=> {"Joe"=>48, "Karen"=>17, "Abash"=>17, "Drake"=>1, "Will"=>23}
irb(main):004:0> arrayOfArrays = nameList.map { |k,v| [k,v] }
=> [["Joe", 48], ["Karen", 17], ["Abash", 17], ["Drake", 1], ["Will", 23]]
irb(main):061:0> def bubble_sort(n, index)
irb(main):062:1> return n if n.length <= 1
irb(main):063:1>
irb(main):064:1* 0.upto(n.length - 1) do |t|
irb(main):065:2* 0.upto(n.length - 2 - t) do |i|
irb(main):066:3* if n[i][index] > n[i + 1][index]
irb(main):067:4> n[i], n[i + 1] = n[i + 1], n[i]
irb(main):068:4> end
irb(main):069:3> end
irb(main):070:2> end
irb(main):071:1>
irb(main):072:1* n
irb(main):073:1> end
=> :bubble_sort
irb(main):075:0> bubble_sort(arrayOfArrays, 1)
=> [["Drake", 1], ["Karen", 17], ["Abash", 17], ["Will", 23], ["Joe", 48]]


Use a suitable sorting algro that handles other data types as well.

Reading this: http://c.learncodethehardway.org/book/ex37.html might give a deeper understanding of hashes and why they can't be sorted directly although can be accessed on O(n) (If I remember correctly it is O(n)).

This post was edited by AbDuCt on Apr 12 2016 10:39pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 12 2016 10:45pm
Quote (AbDuCt @ Apr 13 2016 12:32am)
(If I remember correctly it is O(n)).


a single value given the key can be accessed in more or less O(1) time. (depending on how many collisions are in the hashtable, collision strategy, and whatnot)

This post was edited by carteblanche on Apr 12 2016 10:46pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 12 2016 10:55pm
Quote (carteblanche @ Apr 13 2016 12:45am)
a single value given the key can be accessed in more or less O(1) time. (depending on how many collisions are in the hashtable, collision strategy, and whatnot)


Knew it was something like that.

Anyways if you're into OOP you can also convert your hash into class objects and then apply the sort on them:

Code
require "pp"

class HashObject
attr_reader :string, :int
def initialize(string, int)
@string = string
@int = int
end
end

def bubbleSort(objectsArray, toSort)
for i in 0..objectsArray.size
for j in i+1..objectsArray.size-1
objectsArray[i], objectsArray[j] = objectsArray[j], objectsArray[i] if(objectsArray[i].send(toSort) > objectsArray[j].send(toSort))
end
end
objectsArray
end

nameList = {"Joe"=>48, "Karen"=>17, "Abash"=>17, "Drake"=>1, "Will"=>23}
objectsArray = []

nameList.each do |key, value|
object = HashObject.new(key, value)
objectsArray.push object
end

pp objectsArray
puts
pp bubbleSort(objectsArray, "int")


Output:

Code
[#<HashObject:0x0000000328a0c0 @int=48, @string="Joe">,
#<HashObject:0x0000000328a098 @int=17, @string="Karen">,
#<HashObject:0x0000000328a070 @int=17, @string="Abash">,
#<HashObject:0x0000000328a048 @int=1, @string="Drake">,
#<HashObject:0x0000000328a020 @int=23, @string="Will">]

[#<HashObject:0x0000000328a048 @int=1, @string="Drake">,
#<HashObject:0x0000000328a070 @int=17, @string="Abash">,
#<HashObject:0x0000000328a098 @int=17, @string="Karen">,
#<HashObject:0x0000000328a020 @int=23, @string="Will">,
#<HashObject:0x0000000328a0c0 @int=48, @string="Joe">]


Understanding and implementing polymorphisism, metaprogramming, code guessing, what ever you want to call it is up to the implementer. Essentially you will have to determine all type pairs inside the hash, and then create objects to hold them then apply your custom sort to them.

There are many ways to solve this problem, none of them are going to be fast since it is entirely dependent on your sorting function.

Edit:: Also depending on language, strings may have to be sorted differently. In ruby the comparative operators work on strings as well resulting in me able to send the "string" toSort and the array of objects becomes sorted by the string parameter.

Code
irb(main):001:0> "a" > "b"
=> false
irb(main):002:0> "b" > "a"
=> true


If this were C you would have to implement specific sorting for all the different types you support and more underlying logic to pick the right one.

This post was edited by AbDuCt on Apr 12 2016 10:59pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 12 2016 11:04pm
i just posted the source code that's behind pretty much every languages sort (of course it's written in go)
it's called a quicksort and it's ridiculously faster than a bubblesort

but ya it looks like ur caught up now to all me and cartes posts lol

btw you don't really need arrays or lists in GoLang because of slices


This post was edited by Ideophobe on Apr 12 2016 11:11pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 12 2016 11:05pm


the quick sort is really cool because it dynamically sorts using shell heap and insertion

This post was edited by Ideophobe on Apr 12 2016 11:10pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 12 2016 11:25pm
Did you really just try to educate me on sorting algorithms?

I was answering your question about the different ways to implement sorts on a hash. The sorting algro used doesn't matter as I chose the simplest to implement to show you results.

If anything you need to read up on hashes and sorting algorithms since everyone knows you can't sort in O(n)(besides counting sorts like radix) and that hashes are already linked lists behind the scenes...

Quote
btw you don't really need arrays or lists in GoLang because of slices


BTW you don't need to manually seperate hashes in Ruby because there are already methods that extend hash into enumerable to access sorting functions.

Like really how is that even relevent to anything.

This post was edited by AbDuCt on Apr 12 2016 11:32pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Apr 12 2016 11:33pm
Ideo is just excited to see sorts. he might like this one:


if memory serves, traditional hashtables are more array-based than linked-list-based. the hash function just converts a key into an index for the array. but they can used linked lists for the collisions, so with a poor hashing function or insufficient size it'll pretty much become a linked list. dunno much about the different hashtable variants, though.

This post was edited by carteblanche on Apr 12 2016 11:41pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Apr 12 2016 11:43pm
Quote (carteblanche @ Apr 13 2016 01:33am)
Ideo is just excited to see sorts. he might like this one:
http://www.youtube.com/watch?v=kbzIbvWsDb0

if memory serves, traditional hashtables are more array-based than linked-list-based. the hash function just converts a key into an index for the array. but they can used linked lists for the collisions, so with a poor hashing function or insufficient size it'll pretty much become a linked list. dunno much about the different hashtable variants, though.


I tried to implement something similar for eeproms actually not to long ago. I wanted to be able to store specific data between memory address 0 and ffff by way of hashing their data and converting it to a numeric index. I ended up not doing that because of collision problems and simply made a bitmask of free memory regions and used a lookup table to find my indexes based on hash.

This post was edited by AbDuCt on Apr 12 2016 11:44pm
Member
Posts: 14,631
Joined: Sep 14 2006
Gold: 575.56
Apr 13 2016 12:32am
Quote (carteblanche @ Apr 12 2016 11:33pm)
Ideo is just excited to see sorts. he might like this one:
http://www.youtube.com/watch?v=kbzIbvWsDb0

if memory serves, traditional hashtables are more array-based than linked-list-based. the hash function just converts a key into an index for the array. but they can used linked lists for the collisions, so with a poor hashing function or insufficient size it'll pretty much become a linked list. dunno much about the different hashtable variants, though.


man... i dig it, i shoulda just wrote a jingle sort

but ya they're just just slices indexed by any random datatype
idk what language you work with but slices in golang are really cool, they work just like arrays but they are sized dynamically so they take up exactly the space they need to like an array but can be appended on the fly like a list
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
Apr 13 2016 01:48am
Hold up. Hold up. Hold up.


Is this the same guy that was arguing how Markov Chains were inefficient data structures for representing the AI chat bot you were trying to build? And here he is cumming his pants over quicksort as if he has never seen it before? How does he know what Bayesian Networks and Markov Chains are if he doesn't understand rudimentary sorting? Questionable.

Go Back To Programming & Development Topic List
Prev123Next
Add Reply New Topic New Poll