Solved

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

Posted on 2004-10-11
8
325 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
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

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!

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
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…

691 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