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 8977589
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:24:24+00:00 2026-06-15T19:24:24+00:00

I have a table with these column |Name|Quantity|Unit And I want to get the

  • 0

I have a table with these column

|Name|Quantity|Unit

And I want to get the data sorted by descending size according units of measurement
for example:

|Bread|1.2|Kg
|Pasta|600.21|g
|Flour|200.18|g
|Salt|70.12|mg
|Pepper|60.3|mg
|Venom|700.15|mcg

1.2 is minor than 600.21 but the unit is Kg so in the order priority is before according scale Kg > g > mg >mcg etc.
(assuming that the table doesn’t contains 6000g because the notation is 6Kg)

This code clearly doesn’t works

    public Cursor allDataSorted() {
        try {
            Cursor c = mDb.rawQuery(
                    "SELECT * FROM myTable order by Quantity desc", null);
            if (c != null) {
                c.moveToNext();
            }
            return c;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "allDataSorted>>" + mSQLException.toString());
            throw mSQLException;
        }

    }

    }

And all data in my table are text String so 956 for example is wrongly showed after 99, so I have no idea of How I could solve this problem and the result is totally unsorted.

Any suggestion?

NB

I cannot convert and reconvert the tables,
these are too big, and the adapter is used for too many kind of units to use only a general base unit.

EDIT

Following the suggestion of Ryan Griggs I have included mapperTb table in the same db file

Unit|Multipler
hg|100
g|1
dg|0.1
cg|0.01
mg|0.001
mcg|0.000001

And I have edit the allDataSorted() method in this way:

    public Cursor allDataSorted() {
        try {
            Cursor c = mDb.rawQuery("SELECT *, (CAST(myTable.Quantity as FLOAT) * mapperTb.Multipler) as Quantity FROM myTable INNER JOIN mapperTb ON myTable.Unit = mapperTb.Unit ORDER BY Quantity desc", null);
            if (c != null) {
                c.moveToNext();
            }
            return c;
        } catch (SQLException mSQLException) {
            Log.e(TAG, "allDataSorted>>" + mSQLException.toString());
            throw mSQLException;
        }

    }

    }

but doesn’t works and the Cursor results empty

  • 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-06-15T19:24:25+00:00Added an answer on June 15, 2026 at 7:24 pm

    I recommend that in your sorting routine, you convert all values to a base unit (i.e. Grams). Then you can properly sort. You could create another table which maps each unit to the proper multiplier (i.e Kg = 1000, g = 1) and then calculate the base unit for each entry, and sort by that value. Then you don’t have to worry about things like 6000g vs 6Kg.

    Example: create mapping table “unit_map” with fields ‘unit’ and ‘multiplier’. Add all applicable units and multipliers.
    So the new query becomes:

    SELECT *, (CAST(myTable.Quantity as FLOAT) * unit_map.multiplier) as sort_field FROM myTable INNER JOIN unit_map ON myTable.Unit = unit_map.unit ORDER BY sort_field
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hi have an category table categoryId Categoryname Type these are column name's so i
I have a table which looks like this: Name | Quantity | Unit -------+-------------+----------
lets say we have a table BNK_ACCT_GRP_ST with these columns: Column Name ID Pk
I have two mySQL tables as follows: [product] table P_id | Name | Quantity
I have a table that lists items and has a quantity textbox column that
In my database I have table with a name column containing grades, like 1.
Problem: I have a html table with 4 columns (Name, Price, Quantity, Value). The
I have a variable say, column_name. I want to update a column whose name
I have a Table with these columns Id as integer Name as string Image
I have a table with a column like this that is currently live: name

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.