Skip to content

File Sorting Documentation

Asterios Raptis edited this page Feb 10, 2025 · 3 revisions

Introduction

Sorting file content is essential for many applications, such as arranging log files, processing structured data, and enhancing readability. The file-worker library provides a simple and efficient way to sort file content using customizable comparators.

Imagine you are processing a dataset and need to sort the lines alphabetically or numerically. This library makes such tasks seamless and efficient.

Features Overview

  • Sort file content using custom comparators.
  • Support for different encoding formats.
  • Efficient sorting for large text files.

Getting Started

To include this library in your Java project, add the following dependency:

Gradle Dependency

Replace the variable ${latestVersion} with the current latest version:
Maven Central

Define version in the gradle.properties file:

fileWorkerVersion=${latestVersion}

or in the build.gradle ext area:

    fileWorkerVersion = "${latestVersion}"

Then, add the dependency:

    implementation("io.github.astrapi69:file-worker:$fileWorkerVersion")

Using the libs.versions.toml File

For automatic version updates:

[versions]
file-worker-version=${latestVersion}

[libraries]
file-worker = { module = "io.github.astrapi69:file-worker", version.ref = "file-worker-version" }

Then, add:

    implementation libs.file.worker

Maven Dependency

Maven dependency is now available on Sonatype. Check out the
Sonatype repository
for the latest snapshots and releases.

<properties>
    ...
    <file-worker.version>${latestVersion}</file-worker.version>
    ...
</properties>

<dependencies>
    ...
    <dependency>
        <groupId>io.github.astrapi69</groupId>
        <artifactId>file-worker</artifactId>
        <version>${file-worker.version}</version>
    </dependency>
    ...
</dependencies>

Sorting Files with SortFileExtensions

The SortFileExtensions class provides methods to sort file content based on a custom comparator.

Example: Sorting a File Alphabetically

File file = new File("unsorted.txt");
SortFileExtensions.sort(file, Comparator.naturalOrder(), "UTF-8");

System.out.println("File sorted successfully!");

Example: Sorting a File in Reverse Order

SortFileExtensions.sort(file, Comparator.reverseOrder(), "UTF-8");

System.out.println("File sorted in reverse order!");

Unit Test Coverage

The SortFileExtensionsTest class ensures sorting functionality:

@Test
public void testSort() throws IOException {
    File file = new File("test.txt");
    StoreFileExtensions.toFile(file, "Banana
Apple
Cherry");

    SortFileExtensions.sort(file, Comparator.naturalOrder(), "UTF-8");

    List<String> lines = ReadFileExtensions.readLinesInList(file);
    assertEquals("Apple", lines.get(0));
    assertEquals("Banana", lines.get(1));
    assertEquals("Cherry", lines.get(2));
}

Handling Large Files

Sorting large files can be memory-intensive. This library optimizes sorting by processing files efficiently.

Example: Sorting a Large File

File largeFile = new File("bigdata.txt");
SortFileExtensions.sort(largeFile, Comparator.naturalOrder(), "UTF-8");

System.out.println("Large file sorted successfully!");

Unit Test Coverage

@Test
public void testSortLargeFile() throws IOException {
    File file = new File("large_data.txt");
    StoreFileExtensions.toFile(file, "Zebra
Lion
Elephant
Antelope");

    SortFileExtensions.sort(file, Comparator.naturalOrder(), "UTF-8");

    List<String> lines = ReadFileExtensions.readLinesInList(file);
    assertEquals("Antelope", lines.get(0));
    assertEquals("Elephant", lines.get(1));
    assertEquals("Lion", lines.get(2));
    assertEquals("Zebra", lines.get(3));
}

Conclusion

The file-worker library provides efficient tools for sorting file content using customizable comparators. Whether you need to process small text files or handle large datasets, this library ensures optimal performance and ease of use.

Start using it today to streamline your file sorting operations!

Clone this wiki locally