# StringList : sort by custom algorithm

**URL:** <https://discourse.processing.org/t/stringlist-sort-by-custom-algorithm/36312>\
**Category:** Coding Questions\
**Created:** [April 13, 2022, 9:12am UTC](https://discourse.processing.org/t/stringlist-sort-by-custom-algorithm/36312 "2022-04-13T09:12:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![f.damien12](https://avatars.discourse-cdn.com/v4/letter/f/49beb7/32.png) [@f.damien12](https://discourse.processing.org/u/f.damien12)\
**Post date:** [April 13, 2022, 9:12am UTC](https://discourse.processing.org/t/stringlist-sort-by-custom-algorithm/36312/1 "2022-04-13T09:12:11Z")

</div>

Hello  
In my sketch, a StringList stores some paths to files.  
I want to sort this StringList not by alphabetical order, but by file modification date.

Is there a way to implement a custom sort algorithm ?

Thanks.

---

<div class="post-metadata">

**Author:** ![mnse](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/mnse/32/16520_2.png) [@mnse](https://discourse.processing.org/u/mnse)\
**Post date:** [April 13, 2022, 12:49pm UTC](https://discourse.processing.org/t/stringlist-sort-by-custom-algorithm/36312/3 "2022-04-13T12:49:00Z")

</div>

Hi,  
It depends on what you want to do, but for the first I would switch from StringList to `ArrayList<String>` or better as you know that there are existing Files `ArrayList<File>`.  
Afterwards for the latter:

```auto
arrayListWithFileObjects.sort((f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified()));

```

Or if you want to keep Strings (If it is certain that the files exist) you can do s.th. like.

```auto
arrayListWithStrings.sort((s1,s2) -> Long.compare(new File(s1).lastModified(), new File(s2).lastModified()));

```

for the `ArrayList<String>` version it would be better to go via stream and filter by existence and afterwards do the sort.

Cheers  
— mnse

PS: The solution above is for Processing (\>=4) with java8 support. For older Versions you can use s.th. like:

```
arrayListWithFileObjects.sort(new Comparator<File>() {
	@Override
	public int compare(File f1, File f2) {
		return Long.compare(f1.lastModified(),f2.lastModified()) ;
	}
});
```

---

<div class="post-metadata">

**Author:** ![Chrisir](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/chrisir/32/45_2.png) [@Chrisir](https://discourse.processing.org/u/Chrisir)\
**Post date:** [April 13, 2022, 1:58pm UTC](https://discourse.processing.org/t/stringlist-sort-by-custom-algorithm/36312/4 "2022-04-13T13:58:34Z")

</div>

(I can’t run it, old version of processing here)

```auto
// Files 
ArrayList<File> arrayListWithFileObjects = new ArrayList(); 

String[] filenames; 

//-------------------------------------------------------------------------------------------

void setup () {
  size (1230, 700);

  java.io.File folder = new java.io.File(dataPath(""));
  filenames = folder.list();
  printArray(filenames);

  for (String s1 : filenames) {
    arrayListWithFileObjects.add(new File(s1));
  } 
  println(""); 

  arrayListWithFileObjects.sort((f1, f2) -> Long.compare(f1.lastModified(), f2.lastModified()));
  // arrayListWithStrings.sort((s1, s2) -> Long.compare(new File(s1).lastModified(), new File(s2).lastModified()));

  for (File f1 : arrayListWithFileObjects) {
    println(f1.getPath());
  }

  //
}//func 

void draw () {
  background(0);
}
//

```

---

<div class="post-metadata">

**Author:** ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)\
**Post date:** [April 13, 2022, 4:04pm UTC](https://discourse.processing.org/t/stringlist-sort-by-custom-algorithm/36312/5 "2022-04-13T16:04:31Z")

</div>

[forum.Processing.org/two/discussions/tagged%3FTag=lastmodified().html](https://forum.processing.org/two/discussions/tagged%3FTag=lastmodified().html)

---

<div class="post-metadata">

**Author:** ![f.damien12](https://avatars.discourse-cdn.com/v4/letter/f/49beb7/32.png) [@f.damien12](https://discourse.processing.org/u/f.damien12)\
**Post date:** [April 14, 2022, 7:45am UTC](https://discourse.processing.org/t/stringlist-sort-by-custom-algorithm/36312/6 "2022-04-14T07:45:21Z")

</div>

Hello  
Special thanks to @Chrisir @mnse  
Have a good day
