Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:57:16+00:00 2026-05-26T22:57:16+00:00

I would like to sort two arrays according to their numeric values.For eg apple[]

  • 0

I would like to sort two arrays according to their numeric values.For eg

apple[]  = [apples, green, 9180, 16.19];
orange[] = [oranges, orange color, 9180, 25.18];

I would like to sort it according to the 3rd value in the array i.e 16.19

How will I go about this ?

Note array declaration is for illustrative purposes only.

The result must look like this:

String[] apples =     [apples, green, 9180, 16.19];
String[] oranges =    [oranges, orange color, 9180, 25.18];
String[] grapes =     [grapes, green, 9180, 35.16]
String[] strawberry = [strawberries, red, 9180, 45.36]

Each line is a single separate array. It must be sorted in ascending order according to the last number i.e 16.19, 25.18, 35.16

The array has four fields [apples, green, 9180, 16.19]; Apples: name of the fruit; Green: color of the fruit; 9180: pin code of farm; 16.19: yield of fruits. I must sort these according to the yield of fruits. Hope I am clear.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:57:17+00:00Added an answer on May 26, 2026 at 10:57 pm

    I allowed myself the assumption that you don’t have four variables named oranges, apples, strawberries and grapes. If you do, put them inside an array before sorting.

    Example code to sort an array of arrays of Comparables:

    class a{
    
        public static void main(String[]args){
    
            final int column = 3;
            Comparable[][] a = new Comparable[][]{
                {"oranges", "orange color", 9180, 25.18},
                {"apples", "green", 9180, 16.19},
                {"strawberries", "red", 9180, 45.36},
                {"grapes", "green", 9180, 35.16}
            };
    
            java.util.Arrays.sort(a, 
                new java.util.Comparator<Comparable[]>(){
                    public int compare(Comparable[]a,Comparable[]b){
                        return a[column].compareTo(b[column]);
                    }
            });
    
            for (Comparable[] c : a)
                System.out.println(java.util.Arrays.toString(c));
    
        }
    
    }
    

    Output:

    [apples, green, 9180, 16.19]
    [oranges, orange color, 9180, 25.18]
    [grapes, green, 9180, 35.16]
    [strawberries, red, 9180, 45.36]
    

    If you want something even more generic, you can define an array comparator which takes the comparison column as a parameter in its constructor (see updated example):

    class ArrayComparator implements java.util.Comparator<Comparable[]>{
        private int col;
        public ArrayComparator(int col){
            this.col = col;
        }
        public int compare(Comparable[] o1, Comparable[] o2){
            return o1[col].compareTo(o2[col]);
        }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two arrays of values that I would like to combine - but
I would like to sort a matrix according to a particular column. There is
I have an array with arrays of two elements. Now I would like to
I have a multidimensional array which I would like to sort by two factors:
I would like to sort an array in ascending order using C/C++ . The
I would like to sort an array of strings in a directory, given a
I would like to sort the data of a core data NSSet (I know
I would like to sort the characters in a string. E.g. echo cba |
Given an array 'a' I would like to sort the array by columns sort(a,
I have an array that I would like to sort using a date field

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.