sort an array with float and string

I have a array: 0, a, b, 1.2, 0.2, c
I'd like to sort them by number first, then by alphabetic
result should be:
0, 0.2, 1.2, a, b, c

How can I do this?
xiaoyunwuAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

for_yanCommented:
You define Comparator which aligns the strings this way and then
use method Arrays.sort(array, Comparator)

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xiaoyunwuAuthor Commented:
Thanks!
HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

CEHJCommented:
Neither of those answers will help you unfortunately
xiaoyunwuAuthor Commented:
I've tried out, and it worked. Thanks.
for_yanCommented:
This is the class with static method sortArray, which would sort
your array in the sense you want it (see output):

import java.util.Arrays;

public class MyString implements Comparable<MyString>  {
    String s;
    public MyString(String s){
        this.s = s;
    }

    public String getString(){ return s;}

    public int compareTo(MyString ms1){
     float f = -1.0f;
     float f1 = -1.0f;
      String s1 = ms1.getString();
        boolean first = true;
        boolean second = true;

        try{
            f = Float.parseFloat(s);
        } catch(Exception ex){
            first = false;
        }

        try{
             f1 = Float.parseFloat(s1);
         } catch(Exception ex){
             second = false;
         }

        if(first && !second)return -1;
        if(!first && second)return 1;
        if(first && second){
            if(f<f1)return -1;
            else return 1;
        }
        if((!first) && (!second)){
            return s.compareTo(s1);
        }

      return 1;
    }

    public static String [] sortArray(String [] arr){
        MyString[] myss = new MyString[arr.length];
       for(int j=0; j<arr.length; j++){
           myss[j]= new MyString(arr[j]);
       }

        Arrays.sort(myss);
        String [] ss = new String[myss.length];

          for(int j=0; j<arr.length; j++){
           ss[j]= myss[j].getString();
       }

        return ss;

    }

    public static void main(String[] args) {
        String [] ss = { "0", "c", "b", "1.2", "0.2", "a"};
        ss = MyString.sortArray(ss);
        for(String s: ss){
            System.out.println(s);
        }

    }

}

Open in new window


Output:
0
0.2
1.2
a
b
c

Open in new window

xiaoyunwuAuthor Commented:
Thank you very much, for_yan.
for_yanCommented:
You are always welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.