CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 1999
    Location
    Pacific Ocean, Deep Blue Sea
    Posts
    133

    java sort function ?

    let's say i have an array of integer.
    is there any java sort function so that i can call something like this ?

    java.xxx.sort(myIntegerArray)

    and it returns me an sorted integer array ??

    thanks

    Signature (up to 100 characters) You may use Markup in your signature

  2. #2
    Join Date
    Jan 2000
    Location
    CA, USA
    Posts
    305

    Re: java sort function ?

    Try the foll:

    int[] x = {5, 2, 7, 9, 3};
    com.sun.rmi.io.util.Arrays.sort(x);
    for (int i = 0; i < x.length; i++)
    {
    System.out.println("element no. " + (i + 1) + " = " + x[i]);
    }

    Kannan



  3. #3
    Join Date
    Jan 2000
    Location
    Canada
    Posts
    249

    Re: java sort function ?

    I couldn't find that class, but the following should work:

    int[] x = {5, 2, 7, 9, 3};
    java.util.Arrays.sort(x);
    for (int i = 0; i < x.length; i++) {
    System.out.println("element no. " + (i + 1) + " = " + x);
    }

    -------------------------------------------
    weaver
    icq# 64665116
    Please rate this post.
    http://weaver.x7.htmlplanet.com

  4. #4
    Join Date
    Sep 1999
    Location
    Pacific Ocean, Deep Blue Sea
    Posts
    133

    Re: java sort function ?

    thanks kannan and weaver.
    that's indeed a great help.

    Signature (up to 100 characters) You may use Markup in your signature

  5. #5
    Join Date
    Sep 1999
    Location
    Pacific Ocean, Deep Blue Sea
    Posts
    133

    Re: java sort function ?

    this is holy ****!
    i am using JDK1.1.8
    i can't use java.util.Arrays.sort

    any other solution ?
    pls help me

    Signature (up to 100 characters) You may use Markup in your signature

  6. #6
    Join Date
    Jan 2000
    Location
    Canada
    Posts
    249

    Re: java sort function ?

    I'm sorry, I've never actually seen JDK1.1.8. I'm using 1.2 right now.

    -------------------------------------------
    weaver
    icq# 64665116
    Please rate this post.
    http://weaver.x7.htmlplanet.com

  7. #7
    Join Date
    Sep 1999
    Location
    Madurai , TamilNadu , INDIA
    Posts
    1,024

    Re: java sort function ?


    I have copied someof the sourcecode from java.util.Arrays class and have created a class called "Arrays". Give a look at it. You can use this "Arrays" class in your code.


    public class sortEx{
    public static void main( String[] str ){
    int[] intArray = { 4, 2 , 12 , 5 , 3 , 4 , 10 };
    Arrays.sort( intArray );
    for( int index = 0 ; index < intArray.length ; index++ ){
    System.out.println( intArray[index] );
    }
    }

    }

    class Arrays{
    public static void sort(int[] a) {
    sort1(a, 0, a.length);
    }

    private static void sort1(int x[], int off, int len) {
    // Insertion sort on smallest arrays
    if (len < 7) {
    for (int i=off; i<len+off; i++)
    for (int j=i; j>off && x[j-1]>x[j]; j--)
    swap(x, j, j-1);
    return;
    }

    // Choose a partition element, v
    int m = off + len/2; // Small arrays, middle element
    if (len > 7) {
    int l = off;
    int n = off + len - 1;
    if (len > 40) { // Big arrays, pseudomedian of 9
    int s = len/8;
    l = med3(x, l, l+s, l+2*s);
    m = med3(x, m-s, m, m+s);
    n = med3(x, n-2*s, n-s, n);
    }
    m = med3(x, l, m, n); // Mid-size, med of 3
    }
    int v = x[m];

    // Establish Invariant: v* (<v)* (>v)* v*
    int a = off, bb = a, c = off + len - 1, d = c;
    while(true) {
    while (bb <= c && x[bb] <= v) {
    if (x[bb] == v)
    swap(x, a++, bb);
    bb++;
    }
    while (c >= bb && x[c] >= v) {
    if (x[c] == v)
    swap(x, c, d--);
    c--;
    }
    if (bb > c)
    break;
    swap(x, bb++, c--);
    }

    // Swap partition elements back to middle
    int s, n = off + len;
    s = Math.min(a-off, bb-a ); vecswap(x, off, bb-s, s);
    s = Math.min(d-c, n-d-1); vecswap(x, bb, n-s, s);

    // Recursively sort non-partition-elements
    if ((s = bb-a) > 1)
    sort1(x, off, s);
    if ((s = d-c) > 1)
    sort1(x, n-s, s);

    }

    private static void swap(int x[], int a, int bb) {
    int t = x[a];
    x[a] = x[bb];
    x[bb] = t;
    }

    private static int med3(int x[], int a, int bb, int c) {
    return (x[a] < x[bb] ?
    (x[bb] < x[c] ? bb : x[a] < x[c] ? c : a) :
    (x[bb] > x[c] ? bb : x[a] > x[c] ? c : a));
    }

    private static void vecswap(int x[], int a, int b, int n) {
    for (int i=0; i<n; i++, a++, b++)
    swap(x, a, b);
    }

    }





  8. #8
    Join Date
    Apr 2000
    Location
    india
    Posts
    5

    Re: java sort function ?

    use CompareTo() method & StringTokenizer


  9. #9
    Guest

    Re: java sort function ?

    How about sorting Strings instead of Numbers? Did anyone do so?

    Thanks.


Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured