MeteoriteDB

Log | Files | Refs | README

commit 8636db144e3a6a7d226e8b889a407707ebc36408
parent 58e2c0666b5b95db348f88a322ed584318e1ae07
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Thu,  4 May 2023 23:24:41 +0200

Added sorting by weight algorithm, places nullvalues last.

Diffstat:
Aapp/src/main/java/com/example/project_assignment/comparators/SortByWeight.java | 23+++++++++++++++++++++++
1 file changed, 23 insertions(+), 0 deletions(-)

diff --git a/app/src/main/java/com/example/project_assignment/comparators/SortByWeight.java b/app/src/main/java/com/example/project_assignment/comparators/SortByWeight.java @@ -0,0 +1,23 @@ +package com.example.project_assignment.comparators; + +import com.example.project_assignment.Meteorite; + +import java.util.Comparator; + +public class SortByWeight implements Comparator<Meteorite> { + + @Override + public int compare(Meteorite m1, Meteorite m2) { + //place null values last. + if (m1.getMass() == 0) { + return 1; + } + else if (m2.getMass() == 0) { + return -1; + } else if (m1.getMass() == 0 && m2.getMass() == 0) { + return 0; + } + + return Double.compare(m1.getMass(), m2.getMass()); + } +}