Solved

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

Posted on 2004-10-11
8
298 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
Comment Utility
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
Comment Utility
0
 
LVL 8

Expert Comment

by:kiranhk
Comment Utility
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
 
LVL 12

Expert Comment

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

Hope these link could help you.
Bye, Giant.
0
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 14

Expert Comment

by:sudhakar_koundinya
Comment Utility
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
Comment Utility
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
Comment Utility
Gaint2

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

Expert Comment

by:Giant2
Comment Utility
Thanks.
:)
Happy to help you.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countPairs challenge 7 57
json format text only 4 65
where is session ID cache stored 1 38
Eclipse Neon and jdk 1.8.0 11 64
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
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 how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now