-1

I am using LinkedHashMap to read data from excel file and store them ina table in mysql! How can i sort the LinkedHashMap to store the data with descending IDs? Here is an example of my excel file:

ID Name Salary
50 christine 2349000
43 paulina 1245874
54 laura 4587894

The code below is for store the data of the excel file in the table!

private static LinkedHashMap[] parseExcelColumnData(List sheetData) {

            LinkedHashMap[] tousRows = new LinkedHashMap[sheetData.size() - 1];
            for (int rowCounter = 1; rowCounter < sheetData.size(); rowCounter++) {

                List list = (List) sheetData.get(rowCounter);

                LinkedHashMap<String, Integer> tableFields = new LinkedHashMap(list.size());
                String str;
                String[] tousFields = new String[list.size()];
                int i = 0;

                for (int j = 0; j < list.size(); j++) {
                    Cell cell = (Cell) list.get(j);
                    if (cell != null) {
                        if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
                            tableFields.put(String.valueOf(cell
                                    .getNumericCellValue()), cell.getCellType());
                        } else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
                            tableFields.put(cell.getStringCellValue(), cell
                                    .getCellType());
                        } else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
                            tableFields.put(String.valueOf(cell
                                    .getBooleanCellValue()), cell.getCellType());
                        }
                    }

                }
                tousRows[rowCounter - 1] = tableFields;
            }

            return tousRows;

        }
dedmar
  • 401
  • 3
  • 12
  • 22
  • 3
    possible duplicate of [Sorting LinkedHashMap](http://stackoverflow.com/questions/12184378/sorting-linkedhashmap) – durron597 Apr 29 '13 at 13:01
  • 1
    @AndrewThompson He didn't try typing "sort linkedhashmap" into the search box – durron597 Apr 29 '13 at 13:03
  • I have read about treeMap and then separator but I haven't succeed something!Just why i am asking here. – dedmar Apr 29 '13 at 13:06
  • *"I have read about treeMap and then separator but I haven't succeed"* ***Show us*** your best attempt. – Andrew Thompson Apr 29 '13 at 13:07
  • http://stackoverflow.com/questions/780541/how-to-sort-hash-map, http://stackoverflow.com/questions/12184378/sorting-linkedhashmap, http://stackoverflow.com/questions/1440006/java-sortedmap-treemap-comparable-how-to-use, http://stackoverflow.com/questions/16276686/store-data-in-table-by-ascending-ids/16277537?noredirect=1#16277537 some of links! I searched but because i have problems i am asking. – dedmar Apr 29 '13 at 13:07
  • The code you gave us is not an attempt to sort the `LinkedHashMap`, it is an entirely unrelated part of your code. – durron597 Apr 29 '13 at 13:08
  • @efoikonom Getting information from you is as painful as trying to shampoo a cat. *"because i have problems"* Your problems at effectively communicating your efforts aside, ***what problems?*** – Andrew Thompson Apr 29 '13 at 13:09

1 Answers1

1

According to the documentation, LinkedHashMap "normally" stores key in the order it's added to the map, so you can't really change the ordering.

What you can do instead is extract all the keys and sort it, for example:

LinkedHashMap<Integer, String> map = new LinkedHashMap <Integer, String>();
/* add elements here */
Set<Integer> keys = new TreeSet<Integer>();
keys.addAll(map.keySet());

Set<Integer> keys now contains all the map keys in sorted order because TreeSet stored element based on its natural ordering

gerrytan
  • 40,313
  • 9
  • 84
  • 99