---
title: "Sorting Files Array by LastModefiedTime"  
description: "Sorting Files Array by LastModefiedTime"  
author: "Anonymous User"  
published: 2015-11-27  
updated: 2015-11-27  
canonical: https://www.mindstick.com/forum/33650/sorting-files-array-by-lastmodefiedtime  
category: "android"  
tags: ["android", "java", "sorting"]  
reading_time: 2 minutes  

---

# Sorting Files Array by LastModefiedTime

I want to Sort a [Files](https://www.mindstick.com/articles/23302/the-importance-and-advantage-of-keeping-your-important-files-on-the-cloud) [Array](https://www.mindstick.com/articles/335/jagged-array-in-c-sharp-dot-net) by lastModefiedTime from the [oldest](https://yourviews.mindstick.com/story/1463/some-oldest-languages-in-the-world-still-spoken) to the newest ( first Element [must](https://www.mindstick.com/articles/12978/top-reasons-why-every-magento-ecommerce-store-must-opt-for-mobile-app) be the Oldest and the Last Element the newest).\
i wrote this [Methods](https://www.mindstick.com/articles/13060/runny-nose-remedy-methods-that-work-best) but it didnt work!!\

```
private static void swap(File[] files,int a, int b){    File h = files[a];    files[a]=files[b];    files[b]=h;}public static void fillBoxList(String path){    File dir = new File(path);    File[] files = dir.listFiles();    if (files != null) {        //Box.addToFilesArray(directoryListing[0]);// print the List before Sorting         for(int i =0;i<dfiles.length;i++){            Log.i("LastModDate", new Date(directoryListing[i].lastModified())+"");        }//Beginn of Sorting        for (int i =1;i<files.length;i++) {            Date lastModdate1 = new Date(files[i-1].lastModified());            Date lastModDate2 = new Date(files[i].lastModified());                 if(lastModdate1.compareTo(lastModDate2)>1){                     swap(files,i-1,i);                 }        }// print the List after Sorting        for(int i =0;i<directoryListing.length;i++){            Log.i("SortedLastModDate", new Date(directoryListing[i].lastModified())+"");        }    } else {        Log.e("DircError","directory dont exists");    }}
```

\
can you tell me please what i did [wrong](https://answers.mindstick.com/qa/48468/who-wrote-the-the-wrong-enemy-america-in-afghanistan-2001-2014-and-when)?

## Replies

### Reply by Anonymous User

Your sort algorithm is actually wrong. There should be two loops in a [Bubble sort](https://en.wikipedia.org/wiki/Bubble_sort)\

```
while (true){    boolean swapped = false;    for (int i =1;i<files.length;i++) {        Date lastModdate1 = new Date(files[i-1].lastModified());        Date lastModDate2 = new Date(files[i].lastModified());        if(lastModdate1.compareTo(lastModDate2)>1){              swap(files,i-1,i);              swapped = true;        }    }    if ( !swapped )      break;}
```


---

Original Source: https://www.mindstick.com/forum/33650/sorting-files-array-by-lastmodefiedtime

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
