How to sort in ascending / descending order with SQLite

Overview

When retrieving values from the database, you can sort them in ascending / descending order before retrieving.

How to use

number title
15 ABCD
67 EFGH
22 IJKL
12 MNOP

For example, suppose there is a value in the database (DB) as above

        if(helper == null){
            helper = new TestOpenHelper(getActivity().getApplicationContext());
        }

        if(db == null){
            db = helper.getReadableDatabase();
        }
        Cursor cursor = db.query(
                "testdb",
                new String[] { "number","title" },
                null, //selection
                null, //selectionArgs
                null, //groupBy
                null, //having
                null, //orderBy
        );

If you just want to get the DB in the order of registration, this method is fine.

Specify in order By (sort)

        if(helper == null){
            helper = new TestOpenHelper(getActivity().getApplicationContext());
        }

        if(db == null){
            db = helper.getReadableDatabase();
        }

        String order_by = "number ASC"; //The value you want to sort,ascending order(ASC)Or descending order(DESC)Or

        Cursor cursor = db.query(
                "testdb",
                new String[] { "number","title" },
                null, //selection
                null, //selectionArgs
                null, //groupBy
                null, //having
                order_by
        );

Specify in order By when assigning a value to Cursor → Just specify ASC for ascending order and DESC for descending order!

After that (bonus)

        //Turn the data list
        cursor.moveToFirst();
        for (int i = 0; i < cursor.getCount(); i++) {
       //Write arbitrary processing here
            cursor.moveToNext();
        }
        cursor.close();

Make a note of how to get the sorted data, which is often used.

Recommended Posts

How to sort in ascending / descending order with SQLite
I tried to sort the data in descending order, ascending order / Rails
How to arrange articles (ascending / descending)
How to embed JavaScript variables in HTML with Thymeleaf
How to implement UICollectionView in Swift with code only
How to call functions in bulk with Java reflection
How to switch Tomcat context.xml with WTP in Eclipse
How to use Z3 library in Scala with Eclipse
Organized how to interact with the JDK in stages
[How to insert a video in haml with Rails]
How to delete untagged images in bulk with Docker
Sort Map values in ascending key order in Java TreeMap
How to use JDD library in Scala with Eclipse
How to query Array in jsonb with Rails + postgres
Get location information in Rails and sort in ascending order
How to number (number) with html.erb
How to update with activerecord-import
How to get values in real time with TextWatcher (Android)
Mapping to a class with a value object in How to MyBatis
[Java8] Sort int type array in descending order using stream
Sorting in ascending order in Java (bubble sort: simple exchange algorithm)
How to set up a proxy with authentication in Feign
How to use Lombok in Spring
How to scroll horizontally with ScrollView
How to find May'n in XPath
Sort List in descending order in Java and generate a new List non-destructively
How to hide scrollbars in WebView
How to get started with slim
How to run JUnit in Eclipse
How to deal with 405 Method Not Allowed error in Tomcat + JSP
How to iterate infinitely in Ruby
[Rails] How to write in Japanese
How to run Ant in Gradle
How to make a jar file with no dependencies in Maven
How to master programming in 3 months
How to enclose any character with "~"
How to learn JAVA in 7 days
How to get parameters in Spark
How to switch Java version with direnv in terminal on Mac
How to install Bootstrap in Ruby
How to use mssql-tools with alpine
How to use InjectorHolder in OpenAM
How to run a job with docker login in AWS batch
How to get boolean value with jQuery in rails simple form
How to introduce jQuery in Rails 6
How to rename a model with foreign key constraints in Rails
How to use classes in Java?
How to name variables in Java
How to get along with Rails
How to set Lombok in Eclipse
How to encrypt and decrypt with RSA public key in Java
How to start Camunda with Docker
How to concatenate strings in java
How to install Swiper in Rails
[Personal memo] How to interact with a random number generator in Java
[Rails] How to log in with a name by adding a devise name column
How to test private methods with arrays or variadic arguments in JUnit
How to execute with commands of normal development language in Docker development environment
[Note] I want to get in reverse order using afterLast with JdbcTemplate
[Rails] How to get the user information currently logged in with devise
How to display the text entered in text_area in Rails with line breaks