We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › sort and count strings in an array
Page Index Toggle Pages: 1
sort and count strings in an array (Read 1286 times)
sort and count strings in an array
Feb 15th, 2010, 1:14pm
 
Hi, i am looking for the best and easiest way for the following task.
Ive got an array containing an unknown number of different names... all these names different times. What need is to count them and sort them by how often they appear. To get something like a top ten list of names.

Whats the best approach for this ?

Re: sort and count strings in an array
Reply #1 - Feb 15th, 2010, 1:39pm
 
Using a hashmap maybe? The different strings would be the keys, the number of times they occur would be the values. Then, there should be a method to sort a hashmap based on its values.
Re: sort and count strings in an array
Reply #2 - Feb 15th, 2010, 2:28pm
 
A TreeMap might be more suited, if you make a Comparator comparing values instead of keys.
The advantage is that it is always sorted.
Re: sort and count strings in an array
Reply #3 - Feb 15th, 2010, 3:58pm
 
Thanks to both of you. PhiLho, do you think you might be able to make a little example on how to use this Treemap?
Re: sort and count strings in an array
Reply #4 - Feb 16th, 2010, 6:18am
 
OK, I was wrong. I thought the Comparator could be used to compare on values, but it operates only on keys, not on values.
So I felt back on antiplastik's idea, using a HashMap, and sorting it afterward.
Code:
void setup()
{

String data =
"Hi, i am looking for the best and easiest way for the following task." +
"Ive got an array containing an unknown number of different names... " +
"all these names different times. What need is to count them and sort them by how often they appear. " +
"To get something like a top ten list of names." +
"Whats the best approach for this ? " +
"Using a hashmap maybe? The different strings would be the keys, " +
"the number of times they occur would be the values. " +
"Then, there should be a method to sort a hashmap based on its values.";
String[] names = data.toLowerCase().replaceAll("\\W", " ").replaceAll(" +", " ").split(" ");

Map map = new HashMap();

for (int i = 0; i < names.length; i++)
{
String key = names[i];
NameAndNumber nan = (NameAndNumber) map.get(key);
if (nan == null)
{
// New entry
map.put(key, new NameAndNumber(key, 1));
}
else
{
map.put(key, new NameAndNumber(key, nan.m_number + 1));
}
}

// Sort the collection
ArrayList keys = new ArrayList(map.keySet());
Collections.sort(keys, new NameAndNumberComparator(map));

// List the top ten
int MAX = 10; int count = 0;
Iterator it = keys.iterator();
while (it.hasNext() && count < MAX)
{
String key = (String) it.next();
NameAndNumber nan = (NameAndNumber) map.get(key);
println(key + " -> " + nan.m_number);
count++;
}

exit();
}

class NameAndNumber
{
String m_name;
int m_number;

NameAndNumber(String name, int number)
{
m_name = name;
m_number = number;
}
}

class NameAndNumberComparator implements Comparator, Serializable
{
Map m_map;

NameAndNumberComparator(Map map)
{
m_map = map;
}

//@Override
public int compare(Object o1, Object o2)
{
// Get values associated to the keys to compare
NameAndNumber nan1 = (NameAndNumber) m_map.get(o1);
NameAndNumber nan2 = (NameAndNumber) m_map.get(o2);
// Sort by descending order
return nan2.m_number - nan1.m_number;
}
}
Page Index Toggle Pages: 1