You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

请求按指定优先级排序电站站点信息JSON数组的技术方案

Got it, let's work through how to sort your power station sites by the specified priority levels. Here's a practical, easy-to-implement technical solution:

电站站点按运行状态优先级排序实现方案

1. 明确优先级映射规则

First, we need to formalize the priority hierarchy (note: I'll assume a placeholder type value for "部分系统异常" since it wasn't fully specified—replace it with your actual business-defined value later):

  • 设备断开 (type: "X"): Highest priority (weight = 4)
  • 系统错误 (type: "pause"): Second priority (weight = 3)
  • 部分系统异常 (e.g., type: "warn"): Third priority (weight = 2)
  • 正常运行 (e.g., your sample's type: "grn-blank"): Lowest priority (weight = 1)

2. Core Sorting Logic Implementation

We'll use JavaScript as an example (since JSON processing is common in JS), but the same logic translates easily to other languages like Python or Java.

Example Code (JavaScript)

// Your original site data
const siteData = { 
  "allSitesInfo": [
    { 
      "title": "Site001", 
      "website": "http://www.example.com/", 
      "lat": "22.70896", 
      "lng": "120.35197", 
      "content": { "PR": "87.89", "kWh": "1700" }, 
      "type": "grn-blank" 
    },
    // Add test entries for verification
    { "title": "Site002", "type": "X" },
    { "title": "Site003", "type": "pause" },
    { "title": "Site004", "type": "warn" }
  ] 
};

// Define priority weight mapping
const priorityMap = {
  "X": 4,
  "pause": 3,
  "warn": 2, // Replace with your actual "部分系统异常" type value
  "grn-blank": 1
};

// Custom sorting function
const sortSitesByPriority = (sites) => {
  // Create a copy to avoid mutating the original array
  return [...sites].sort((siteA, siteB) => {
    // Get priority weight, default to 1 (normal state) if type is unrecognized
    const priorityA = priorityMap[siteA.type] || 1;
    const priorityB = priorityMap[siteB.type] || 1;
    // Sort in descending order (higher priority first)
    return priorityB - priorityA;
  });
};

// Execute sorting
const sortedSites = sortSitesByPriority(siteData.allSitesInfo);
console.log(sortedSites);

3. Cross-Language Adaptation & Extensions

  • Python Version: The logic is identical—use a priority dictionary and the sorted() function with a custom key:
priority_map = {
    "X": 4,
    "pause": 3,
    "warn": 2,
    "grn-blank": 1
}

sorted_sites = sorted(
    site_data["allSitesInfo"],
    key=lambda site: priority_map.get(site["type"], 1),
    reverse=True
)
  • Dynamic Adjustments: If you need to update priorities later, just modify the weight values in the mapping table—no changes to the core sorting logic required.
  • Unrecognized Types: The code defaults unlisted type values to the lowest priority. You can tweak this behavior (e.g., log a warning, assign to a specific category) based on your business needs.

内容的提问来源于stack exchange,提问作者Penny Liu

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:49:21