Repository: webforj/webforj
Pull request #940 (2025-03-21 12:20:36)

fix: When attempting to sort a column that contains null values, an error occurs

github-actions[bot]: <h1>Dependency Review</h1>
✅ No vulnerabilities or license issues or OpenSSF Scorecard issues found.<h2>OpenSSF Scorecard</h2>
<table><tr><th>Package</th><th>Version</th><th>Score</th><th>Details</th></tr>
</table><h2>Scanned Files</h2>
<ul>


<!-- dependency-review-pr-comment-marker -->
sonarqubecloud[bot]: ## [![Quality Gate Passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/qg-passed-20px.png 'Quality Gate Passed')](https://sonarcloud.io/dashboard?id=webforj&pullRequest=940) **Quality Gate passed**  
Issues  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 New issues](https://sonarcloud.io/project/issues?id=webforj&pullRequest=940&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/accepted-16px.png '') [0 Accepted issues](https://sonarcloud.io/project/issues?id=webforj&pullRequest=940&issueStatuses=ACCEPTED)

Measures  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=webforj&pullRequest=940&issueStatuses=OPEN,CONFIRMED&sinceLeakPeriod=true)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Coverage on New Code](https://sonarcloud.io/component_measures?id=webforj&pullRequest=940&metric=new_coverage&view=list)  
![](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/passed-16px.png '') [0.0% Duplication on New Code](https://sonarcloud.io/component_measures?id=webforj&pullRequest=940&metric=new_duplicated_lines_density&view=list)  
  
[See analysis details on SonarQube Cloud](https://sonarcloud.io/dashboard?id=webforj&pullRequest=940)


github-actions[bot]: ### Code Coverage
|Overall Project|74.56% **`-0.01%`**|:green_apple:|
|:-|:-|:-:|
|Files changed|53.33%|:x:|
<br>

|Module|Coverage||
|:-|:-|:-:|
|webforj-data|85.28% **`-0.16%`**|:x:|

<details>
<summary>Files</summary>

|Module|File|Coverage||
|:-|:-|:-|:-:|
|webforj-data|[CompositeComparator.java](https://github.com/webforj/webforj/blob/a80da6f7dca14dc0b8f9c091639d293de4cd99d7/webforj-data%2Fsrc%2Fmain%2Fjava%2Fcom%2Fwebforj%2Fdata%2Frepository%2FCompositeComparator.java)|92.76% **`-4.61%`**|:x:|

</details>

Patch:
diff --git a/webforj-data/src/main/java/com/webforj/data/repository/CompositeComparator.java b/webforj-data/src/main/java/com/webforj/data/repository/CompositeComparator.java
index 9a924b60..64dde9f6 100644
--- a/webforj-data/src/main/java/com/webforj/data/repository/CompositeComparator.java
+++ b/webforj-data/src/main/java/com/webforj/data/repository/CompositeComparator.java
@@ -68,14 +68,10 @@ public class CompositeComparator<T> implements Comparator<T> {
         return 0;
       }
 
-      if (o1 == null) {
+      if (o1 == null || o2 == null) {
         return direction == OrderCriteria.Direction.ASC ? -1 : 1;
       }
 
-      if (o2 == null) {
-        return direction == OrderCriteria.Direction.ASC ? 1 : -1;
-      }
-
       try {
         if (criterion.getComparator() != null) {
           // Use the custom comparator if provided
@@ -120,6 +116,11 @@ public class CompositeComparator<T> implements Comparator<T> {
    */
   protected <V extends Comparable<V>> int compareWithDirection(V value1, V value2,
       OrderCriteria.Direction direction) {
+
+    if (value1 == null || value2 == null) {
+      return direction == OrderCriteria.Direction.ASC ? -1 : 1;
+    }
+
     int comparison = value1.compareTo(value2);
     return direction == OrderCriteria.Direction.ASC ? comparison : -comparison;
   }


Test patch:
diff --git a/webforj-data/src/test/java/com/webforj/data/repository/CompositeComparatorTest.java b/webforj-data/src/test/java/com/webforj/data/repository/CompositeComparatorTest.java
index c9a3b21b..38c12c32 100644
--- a/webforj-data/src/test/java/com/webforj/data/repository/CompositeComparatorTest.java
+++ b/webforj-data/src/test/java/com/webforj/data/repository/CompositeComparatorTest.java
@@ -148,7 +148,7 @@ class CompositeComparatorTest {
         new CompositeComparator<>(new OrderCriteriaList<>(lengthCriteria));
 
     assertTrue(comparator.compare(null, "abc") < 0);
-    assertTrue(comparator.compare("abc", null) > 0);
+    assertTrue(comparator.compare("abc", null) < 0);
     assertEquals(0, comparator.compare(null, null));
   }
 
@@ -168,7 +168,7 @@ class CompositeComparatorTest {
     Person person2 = new Person("Charlotte");
 
     assertTrue(comparator.compare(null, person1) < 0);
-    assertTrue(comparator.compare(person2, null) > 0);
+    assertTrue(comparator.compare(person2, null) < 0);
     assertEquals(0, comparator.compare(person1, person1));
   }