BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Sorting float array in Java example

Last Updated: September 11, 2022 by Chaitanya Singh | Filed Under: java

In this tutorial we are gonna learn how to sort a float array.

import java.util.Arrays;

class SortingFloatArrayExample {

  public static void main(String[] args) {
 
    // Creating a Float Array
    float[] floatArray = 
       new float[] { 21.1f, 9.9f, 9.8f, 7.5f, 2.1f };
 
    // Displaying Array before Sorting
    System.out.println("**Float Array Before Sorting**");
    for (float temp: floatArray){
       System.out.println(temp);
    }

    // Sorting the Array
    Arrays.sort(floatArray);
    System.out.println("**Float Array After Sorting**");
    for (float temp: floatArray){
       System.out.println(temp);
    }
 
    // Another Char Array
    float[] floatArray2 = 
       new float[] { 1.9f, 19.9f, 9.8f, 27.5f, 2.1f, 3.5f, 1.1f};
 
    // Selective Sorting
    /* public static void sort(float[] a, int fromIndex,
     * int toIndex): Sorts the specified range of the 
     * array into ascending order. The range to be sorted 
     * extends from the index fromIndex, inclusive, to the 
     * index toIndex, exclusive. If fromIndex == toIndex, 
     * the range to be sorted is empty.
     */
    Arrays.sort(floatArray2, 2, 5);

    // Displaying array after selective sorting 
    System.out.println("**Selective Sorting**");
    for (float temp: floatArray2){
       System.out.println(temp);
    }
  }
}

Output:

**Float Array Before Sorting**
21.1
9.9
9.8
7.5
2.1
**Float Array After Sorting**
2.1
7.5
9.8
9.9
21.1
**Selective Sorting**
1.9
19.9
2.1
9.8
27.5
3.5
1.1

References:
sort(float[] array) method – javadoc
sort(float[] array, int fromIndex, int toIndex) method – javadoc

Top Related Articles:

  1. Java – Finding minimum and maximum values in an array
  2. Sort byte array in Java
  3. Sorting char array in Java example
  4. How to Convert an array to ArrayList in java
  5. ArrayList clone() method in Java

Tags: Java-Array

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap