邹能昇 4 kuukautta sitten
vanhempi
commit
142afacb61
1 muutettua tiedostoa jossa 17 lisäystä ja 17 poistoa
  1. 17 17
      nova-tourism/pages/homestay/homestay-detail/index.js

+ 17 - 17
nova-tourism/pages/homestay/homestay-detail/index.js

@@ -208,29 +208,29 @@ Page({
             roomItem.isroom = count < roomItem.total; // 简化判断
             return roomItem;
         }));
-
         // 对 roomList 进行排序
-        roomList.sort((a, b) => {
-            // 首先比较 isroom,true 排在前面
-            if (a.isroom === b.isroom) {
-                // 如果 isroom 相同,按 remaining 排序
-                if (a.remaining === 0 && b.remaining !== 0) {
-                    return 1; // a 排后面
-                }
-                if (a.remaining !== 0 && b.remaining === 0) {
-                    return -1; // a 排前面
-                }
-                // 如果两个房间的 remaining 都不为 0,按数量升序排列
-                return a.remaining - b.remaining;
-            }
-            return a.isroom ? -1 : 1; // true 排在前面,false 排在后面
-        });
+        roomList.sort(this.compareFunction);
 
         this.setData({
             roomList
         });
         console.log('房间', this.data.roomList);
     },
+    //排序
+    compareFunction(a, b) {
+        // 先比较 isroom 的值
+        if (a.isroom !== b.isroom) {
+            return a.isroom ? -1 : 1;
+        }
+        // 如果 isroom 相同,再比较 index 是否有值
+        const hasIndexA = a.index !== undefined;
+        const hasIndexB = b.index !== undefined;
+        if (hasIndexA !== hasIndexB) {
+            return hasIndexA ? -1 : 1;
+        }
+        // 如果 index 都有值或者都没值,按 index 大小排序(都没值时顺序其实不变)
+        return (a.index || 0) - (b.index || 0);
+    },
     async checkOrderCount(roomId, start, end) {
         const startTime = new Date(start);
         const endTime = new Date(end);
@@ -323,7 +323,7 @@ Page({
                                 color: '#ffffff',
                                 bgColor: '#7F56B2',
                                 padding: 2,
-                                fontSize:12,
+                                fontSize: 12,
                                 borderRadius: 5,
                                 display: 'ALWAYS'
                             }