如何按升序排列一个字典数组,然后重新排序其他数组?

时间:2016-04-13 20:59:14

标签: arrays swift sorting dictionary

我正在尝试对字典(类型为[String:[String]])进行排序,以便一个键按升序排列,一旦键被排序,我也想对其他数组进行排序。

这就是我的意思。

Create Procedure sp_EmpBonus (@BonusAmt int)
AS
Select FirstName, 
    LastName,
    Title, 
    Salary, 
    Salary * CAST(TotalBonus as int)
From Employee   
Where CAST(TotalBonus as int) = @BonusAmt

sp_EmpBonus 10

我如何将var dictionary = ["timeStamp":[String],"condition":[String]] //Dict to sort dictionary["timeStamp"] = ["123","345","456","234"] dictionary["condition"] = ["dry","wet","very wet","dry"] dictionary["timeStamp"] = dictionary["timeStamp"]!.sort() print("\(dictionary["timeStamp"]!)") //Returns["123","234","345","456"] 排序为dictionary["condition"]

2 个答案:

答案 0 :(得分:1)

我会创建一个简单的结构,以便您的属性相关联并可以一起排序

struct condition {
    var timeStamp: Int
    var description: String
}

var conditionArray = [condition]()
conditionArray.append(condition(timeStamp: 123, description: "dry"))
conditionArray.append(condition(timeStamp: 345, description: "wet"))
conditionArray.append(condition(timeStamp: 456, description: "very wet"))
conditionArray.append(condition(timeStamp: 234, description: "dry"))
let sortedArray = conditionArray.sort() {return $0.timeStamp < $1.timeStamp}

答案 1 :(得分:0)

首选的方法是给它一个像@Russel Austin在他的回答中建议的正确结构。但是你也可以使用Swift高阶函数获得一些乐趣:

Array(0..<dictionary["timeStamp"]!.count)

timeStamp生成一个整数数组,分别为0,1,2,3 ......长度为.map { ... }

.sort{ ... }将字典中的数据拉入时间戳和条件的元组

public View getView(int position, View convertView, ViewGroup parent) { View row; row = convertView; final dataHandler handler; if(convertView == null){ LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate( R.layout.row_layout,parent, false); handler = new dataHandler(); handler.pictures = (ImageView) row.findViewById(R.id.pictures); handler.name = (TextView) row.findViewById(R.id.picturename); handler.qty= (EditText) row.findViewById(R.id.qty); handler.add = (Button) row.findViewById(R.id.btnplus); handler.minus = (Button) row.findViewById(R.id.btnminus); row.setTag(handler); }else{ handler = (dataHandler) row.getTag(); } final PSP psp; psp =(PSP) this.getItem(position); Picasso.with(getContext()).load(psp.getPicture()).resize(200, 155).into(handler.pictures); handler.name.setText(psp.getName() + "\n ₱" + psp.getPrice()); handler.qty.setText("0"); img = psp.getPicture(); handler.add.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String pricex = handler.qty.getText().toString(); int xx = Integer.parseInt(pricex); int total = xx + 1; String totalx = Integer.toString(total); handler.qty.setText(totalx); map.put("" + handler.name.getText().toString(), " " + handler.qty.getText().toString()); ShowHashMapValue(); } }); handler.minus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String pricex = handler.qty.getText().toString(); int xx = Integer.parseInt(pricex); if(xx > 0 ){ int total = xx-1; String totalx = Integer.toString(total); handler.qty.setText(totalx); map.put("" + handler.name.getText().toString(), " " + handler.qty.getText().toString()); }} }); return row; } 按时间戳

对元组数组进行排序
相关问题