简体   繁体   中英

Sorting Lines Based on words in them as keys

 Odrer     ID    Descrption    Type 

Envelope  K205    Green       Paper     >>>>String Line with space in between
Box       J556    Yellow      Thermocol >>>>String Line with space in between
Envelope  L142    White       Plastic   >>>>String Line with space in between

I have a table something like above and I have to provide options sort the data based on the column name, like "sort by Order", "Sort By Type" something like that plz suggest me the best ideas to achieve this in Java... The table rows are strings with space separating each touple..

You have to develop your custom Comparator. For example something like the following.

class ColumnComparator implements Comparator<String> {
    private int column;
    ColumnComparator(int column) {
         this.column = column;
    }
    public int compare(String s1, String s2) {
        return getColumn(s1).compareTo(getColumn(s2));
    }
    private String getColumn(String line) {
        return line.split("\\s+")[column];
    }

Then you can use Arrays.sort() or Collections.sort() passing there array or collection of your lines and instance of this comparator. I leave it for you to implement logic that finds the column index by column name.

}

Represent each row in your table as an object with order, id, description and type attributes. Add each object to a collection such as a list. You can then create different comparators such as an OrderComparator and a TypeComparator to sort the collection based on order or type respectively.

For example, here is what your data class might look like:

public class Data{
    private final String order;
    private final String id;
    private final String description;
    private final String type;

    public Data(String order, String id, String description, String type) {
        this.order=order;
        this.id=id;
        this.description=description;
        this.type=type;
    }

    /** getter methods here **/
}

Now, you would add them to a list and use a specific comparator to sort it:

//add data to a list
List<Data> list = new ArrayList<Data>();
list.add(new Data("Envelope","K205","Green","Paper"));
list.add(new Data("Box","J556","Yellow","Thermocol"));
list.add(new Data("Envelope","L142","White","Plastic"));

//create a comparator to sort by type
Comparator<Data> typeComparator = new Comparator<Data>() {
    @Override
    public int compare(Data o1, Data o2) {
        return o1.getType().compareTo(o2.getType());
    }
};

//sort the list
Collections.sort(list, typeComparator);

Outline of a possible approach (assuming the data does not come from a database):

  1. Represent your data as a class with the appropriate fields:

     class YourData { private final String Order; private final String Id; private final String Description; private final String Type; // add getters, setters and appropriate constructors } 

    Use the appropriate data-types. You can use a Scanner to parse the data.

  2. Write a Comparator for each attribute you want to order by. If you want to reverse the ordering, you can use Collections.reverseOrder .

  3. Put your data into an ArrayList , and use Collections.sort with the appropriate Comparator to achieve the desired ordering.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM