01: /* Copyright (C) 2003 Internet Archive.
02: *
03: * This file is part of the Heritrix web crawler (crawler.archive.org).
04: *
05: * Heritrix is free software; you can redistribute it and/or modify
06: * it under the terms of the GNU Lesser Public License as published by
07: * the Free Software Foundation; either version 2.1 of the License, or
08: * any later version.
09: *
10: * Heritrix is distributed in the hope that it will be useful,
11: * but WITHOUT ANY WARRANTY; without even the implied warranty of
12: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13: * GNU Lesser Public License for more details.
14: *
15: * You should have received a copy of the GNU Lesser Public License
16: * along with Heritrix; if not, write to the Free Software
17: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18: *
19: * Created on Jul 21, 2003
20: *
21: * To change the template for this generated file go to
22: * Window>Preferences>Java>Code Generation>Code and Comments
23: */
24: package org.archive.crawler.util;
25:
26: import java.util.ArrayList;
27: import java.util.Arrays;
28: import java.util.HashMap;
29:
30: public class Sorts {
31:
32: // Sorts by value not key
33: public static StringIntPair[] sortStringIntHashMap(
34: HashMap<String, Integer> hm) {
35: String[] keys = hm.keySet().toArray(new String[hm.size()]);
36: Integer[] values = hm.values().toArray(new Integer[hm.size()]);
37:
38: ArrayList<StringIntPair> unsortedList = new ArrayList<StringIntPair>();
39:
40: for (int i = 0; i < keys.length; i++)
41: unsortedList.add(i, new StringIntPair(keys[i], values[i]));
42:
43: StringIntPair[] sortedArray = unsortedList
44: .toArray(new StringIntPair[unsortedList.size()]);
45: Arrays.sort(sortedArray, new StringIntPairComparator());
46:
47: return sortedArray;
48: }
49:
50: }
|