Dc Redwing
Dc Redwing

Reputation: 1791

Java Sorting value by unique index number

I am studying Java and sorting.

I have a question about tracking the index number for duplicate values.

For example, we have table and I put all data into the ArrayList, like this:

ArrayList = {FOO , AA, BOB, AA, BOB}

Index | Value
1     |  FOO
2     |  AA
3     |  BOB
4     |  AA
5     |  BOB

Now I would like to sort the data:

Index | Value
2     | AA
4     | AA
3     | BOB
5     | BOB
1     | FOO

Is there any way to keep unique index and sort data?

Thanks.

Upvotes: 0

Views: 160

Answers (4)

someone
someone

Reputation: 6572

You could have this kind of format

import java.util.ArrayList;
 import java.util.Collections;


 public class MyData implements Comparable<MyData>{

private Integer index;
private String value;




public MyData(Integer index, String value) {
    this.index = index;
    this.value = value;
}




/**
 * @return the index
 */
public Integer getIndex() {
    return index;
}




/**
 * @param index the index to set
 */
public void setIndex(Integer index) {
    this.index = index;
}




   /**
     * @return the value
    */
    public String getValue() {
        return value;
     }




    /**
    * @param value the value to set
    */
    public void setValue(String value) {
        this.value = value;
     }




public int compareTo(MyData o) {
    int compare = this.value.compareTo(o.getValue());

    if(compare ==0){
        compare = this.index.compareTo(o.getIndex());
    }
    return compare;
}

/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    return "MyData [index=" + index + ", value=" + value + "]";
}




public static void main(String arg[]){


    List<MyData> mySet = new ArrayList<MyData>();
    mySet.add(new MyData(1,"FOO"));
    mySet.add(new MyData(2,"AA"));
    mySet.add(new MyData(3,"BOB"));
    mySet.add(new MyData(4,"AA"));
    mySet.add(new MyData(5,"BOB"));
    Collections.sort(mySet);
    System.out.println(mySet);

}

}

Upvotes: 0

Jay
Jay

Reputation: 27474

Create an object that will hold both the index and the text string. Like

public class MyThing
{
  public int index;
  public String text;
}

Then instead of creating an ArrayList of Strings, create an ArrayList of these objects.

I don't know what you're using to sort them. If you're writing your own sort, than you can simply do your comparisons against the "text" member of each object rather than against the string object itself. If you're using, say, Arrays.sort to sort it, then you need to implement Comparable. Namely:

public class MyThing implements Comparable<MyThing>
{
  public int index;
  public String text;

  public int compareTo(MyThing that)
  {
    return this.text.compareTo(that.text);
    // May need to be more complex if you need to handle nulls, etc
  }

  // If you implement compareTo you should override equals ...
  public boolean equals(Object that)
  {
    if (!(that instanceof MyThing))
    {
      return false;
    }
    else
    {
      MyThing thatThing=(MyThing)that;
      return this.text.equals(thatThing.text);
    }
  }
}

Etc. You may need other stuff depending on what you're trying to do.

Upvotes: 0

yousafsajjad
yousafsajjad

Reputation: 973

You can use the following to sort Arraylist or any Collection subclass.

// unsortstList is an ArrayList
Collections.sort(unsoredtList);

Upvotes: 0

Jigar Joshi
Jigar Joshi

Reputation: 240900

Create a class

class DataHelper{
  private String name;
  private int index;
  // other stuff
}

and create List<DataHelper> and write a Comparator to sort DataHelpers

Upvotes: 1

Related Questions