Solved

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

Posted on 2004-10-11
8
316 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

713 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