How can I sort excel file by the first column? java

i have to make a program where we read data from excel files as the one here and store them in a table in mysql with descending ID. I have made the program to read the data and print them in console using APACHE POI, create the table by the first row of the excel file and then store the rest data. But the storing I would like to happen by their IDs. So I made a function where first I will read the first column where the IDs are and then read the rest data using LinkedHashMap.

How i would do the sorting in IDs?Could anyone help me? I have put spaces where i think the changes should be made.

Answers


Add each TableRow to your List. Use Collections sort() method to sort the data and then iterate through it and insert.

You can customize the compareTo() to determine the order.

@Override
public int compareTo(TableRow o) {
    // for descending order
    return this.getID().compareTo(o.getID())*(-1);
}

Using Comparator, you can do something like this :

 new Comparator<TableRow>() {
        @Override
        public int compare(TableRow o1, TableRow o2) {
             // for descending order
            return o1.getID().compareTo(o.getID())*(-1);
        }
  }

You can use TreeMap. pass the id coulumn value of the excelsheet as the the KeyId for treemap. It will automatically sort your data ...


Need Your Help

How can I save a client-side only webpage with text inputs?

javascript html

I have made a simple webpage with a Javascript backend. I want the user to be able to enter in a bunch of data in form fields, and then save the page. PDF, HTML, doesn't matter, I just want to be...

IEnumerable Ajax.Beginform to Controller is null

model controller null ajax.beginform

I have a view contains a list of users that is passed through the model. What I want to do is have a button for each user using which passes the userId back to the controller so that I could store ...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.