Solved

How do I sort a String Array based on a key of substrings?

Posted on 2004-10-11
8
314 Views
Last Modified: 2010-03-31
I create a key of substrings from a String Array.  I want to sort my String Array based on the key I created.


String Array example:                       key of substrings:
123  xxxxxx  456 xxx 789 xxxxxx        123456789
123  xxxxxx  789 xxx 456 xxxxxx        123789456
123  xxxxxx  456 xxx 789 xxxxxx        123456789
123  xxxxxx  789 xxx 456 xxxxxx        123789456

Desired output:
123  xxxxxx  456 xxx 789 xxxxxx        
123  xxxxxx  456 xxx 789 xxxxxx        
123  xxxxxx  789 xxx 456 xxxxxx        
123  xxxxxx  789 xxx 456 xxxxxx

Any suggestions???      

I'm rather new to Java so please explain and show code...



0
Comment
Question by:seckel
8 Comments
 
LVL 12

Expert Comment

by:Giant2
ID: 12277467
use the sort method and create your own Comparator.
See here:
http://www.javaalmanac.com/egs/java.lang/Sortable.html
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12277498
0
 
LVL 8

Expert Comment

by:kiranhk
ID: 12277517
Hi,
if you put your data into an array list, you can sort the same using the Collections.sort
        ArrayList result= new ArrayList();
result.add("456");
result.add("123");
        Collections.sort(result);


else you can implement the comparable interface for your custom class/value object

class Content implements Comparable {

     * @param other: Any object to be compared with.
     * @return true if the all the attributes of the two answer
     *             objects are the same
     */
    public boolean equals(Object other)
    {
          if (this==other) return true;
          if (other==null) return false;
          if (this.getClass() != other.getClass() ) return false;
          Content sameClass = (Content)other;
              return this.author.equals  (sameClass.author)&&
               this.title.equals(sameClass.title) &&
    }

      public int compareTo(Object o) {
            Content c = (Content)o;
//you can compare the title or all of your variables in ur class
            return this.title.compareTo(c.title);
      }



hope this helps.

Kiran
0
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 
LVL 12

Expert Comment

by:Giant2
ID: 12277532
see even here:
http://java.sun.com/docs/books/tutorial/collections/interfaces/order.html

Hope these link could help you.
Bye, Giant.
0
 
LVL 14

Expert Comment

by:sudhakar_koundinya
ID: 12277878
Use this idea
import java.util.*;

public class SortTest {

  public static void main(String s[]) {
    String[] arr1 = {
        "123", "456", "789"};
    String key = "123456789";
    String[] arr2 = {
        "123", "456", "123"};
    String key1 = "123456123";
    SortArray array = new SortArray(arr1, key);
    array = new SortArray(arr2, key1);
    Vector vect = array.getArray();
    System.err.println(vect.size());
    SortArray[] sortArray = (SortArray[]) vect.toArray(new SortArray[0]);
    java.util.ArrayList vect1 = Collections.list(sortArray[0].ht.keys());
    Collections.sort(vect1);
    for(int i=0;i<vect1.size();i+=1)
    {
     
     for(int j=0;j<sortArray.length;j++)
     {
     
       
       if(vect1.get(i).toString().equals(sortArray[j].key))
       {
         System.err.print("Key :" + sortArray[j].key+" ");
         for(int k=0;k<sortArray[j].array.length;k++)
         {
           System.err.print(sortArray[j].array[k]+" ");
         }
         System.err.println();
       }
     }
    }

  }

}

class SortArray {
  static final Vector vect = new Vector();

  SortArray(String[] array, String key) {
    this.array = array;
    this.key = key;
    vect.add(this);
    ht.put(key, key);
  }

  Vector getArray() {
    return vect;
  }

  String[] array = null;
  String key = null;
  static final Hashtable ht = new Hashtable();
}
0
 
LVL 12

Accepted Solution

by:
Giant2 earned 500 total points
ID: 12284125
The basically idea of Comparator is:
You have a collection (an Array in your case).
Create a class Comparator that say when one object is less, equal, greater than other (see the link I post).
Your data are formed by a key(to sort) and a data. So you can create an object representing this structure. When use Collection.sort(,..yourComparator), your Comparator must decide is one object is less... than other. And the job is done!

Sorry, but we cannot post a full code (it's out of EE mission). We can give you ideas and eventually check why your code not goes.

Hope this could help you.
Bye, Giant.
0
 

Author Comment

by:seckel
ID: 12286204
Gaint2

I'll try your idea today and let you know....
0
 
LVL 12

Expert Comment

by:Giant2
ID: 12295000
Thanks.
:)
Happy to help you.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How to convert String matching to regex in java 4 49
null output 3 35
SequenceInputStream example 3 19
restrict decimal places for double datatype 10 19
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question