如何在两个arrayList中按字母顺序排序

时间:2014-01-02 05:45:32

标签: android sorting

如何对android中的字母顺序进行排序。

Button sort=(Button)findViewById(R.id.sort)
sort.setonClickListener()
Public void onclick(view v)
{

Collections.sort(str3, String.CASE_INSENSITIVE_ORDER);

                        customlist      = new CustomResourseList(Resourse1.this, str3,str4);
                      resourseList.setAdapter(customlist);
}

但我用这种方式意味着我得到了结果,

排序前的

mani
180 cross street

Level wedding planner
alphama

ziaasdf
234fasd

排序后:

Level wedding planner
180 cross street

mani
alphama

ziaasdf
234fasd

但我希望结果如下

Level wedding planner
alphama

mani
180 cross street

ziaasdf
234fasd

如何对编码进行排序?

3 个答案:

答案 0 :(得分:2)

尝试自定义Comparator

 Collections.sort(list, new Comparator<String>() 
 {
    @Override
    public int compare(String s1, String s2) 
    {
        return s1.compareToIgnoreCase(s2);
    }
});

答案 1 :(得分:-1)

import java.util.*;

public class StringArraySort {
    public static void main(String[] args) {
        String[] MyStringArray = { "mani 180 cross street",
                "Level wedding planner alphama", "ziaasdf 234fasd" };
        Arrays.sort(MyStringArray, String.CASE_INSENSITIVE_ORDER);
        System.out.println(Arrays.toString(MyStringArray));
    }
}

output:[Level wedding planner alphama, mani 180 cross street, ziaasdf 234fasd]

答案 2 :(得分:-1)

尝试以下解决方案

package com.caloriapp.nutrition;

import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import com.caloriapp.R;

/**
 * @author ACCESS
 * 
 */
public class SortActivity extends Activity {
    // String[] _StrOne = new String[] { "mani", "Level wedding planner",
    // "ziaasdf" };
    // String[] _StrTwo = new String[] { "180 cross street", "alphama",
    // "234fasd" };
    Map<String, String> olympic2012 = new HashMap<String, String>();
    Map<String, String> sorted = new HashMap<String, String>();
    ListView listView1;
    LayoutInflater LI;
    Iterator i;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sort);
        LI = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        listView1 = (ListView) findViewById(R.id.listView1);
        // Arrays.sort(_StrOne);
        //
        // for (String c : _StrOne) {
        // System.out.println(c);
        // }

        olympic2012.put("mani", "180 cross street");
        olympic2012.put("level wedding planner", "alphama");
        olympic2012.put("ziaasdf", "234fasd");
        olympic2012.put("russia", "234fasd");
        // olympic2012.put("Australia", 4); //adding duplicate value

        // printing hashtable without sorting
        System.out.println("Unsorted Map in Java : " + olympic2012);

        // sorting Map e.g. HashMap, Hashtable by keys in Java
        sorted = sortByKeys(olympic2012);
        System.out.println("Sorted Map in Java by key: " + sorted);
        Set set = sorted.entrySet();
        // Get an iterator
        i = set.iterator();
        listView1.setAdapter(new CustomAdapter());

    }

    public static <K extends Comparable, V extends Comparable> Map<K, V> sortByKeys(
            Map<K, V> map) {
        List<K> keys = new LinkedList<K>(map.keySet());
        Collections.sort(keys);

        // LinkedHashMap will keep the keys in the order they are inserted
        // which is currently sorted on natural ordering
        Map<K, V> sortedMap = new LinkedHashMap<K, V>();
        for (K key : keys) {
            sortedMap.put(key, map.get(key));
        }

        return sortedMap;
    }

    /*
     * Java method to sort Map in Java by value e.g. HashMap or Hashtable throw
     * NullPointerException if Map contains null values It also sort values even
     * if they are duplicates
     */
    public static <K extends Comparable, V extends Comparable> Map<K, V> sortByValues(
            Map<K, V> map) {
        List<Map.Entry<K, V>> entries = new LinkedList<Map.Entry<K, V>>(
                map.entrySet());

        Collections.sort(entries, new Comparator<Map.Entry<K, V>>() {

            @Override
            public int compare(Entry<K, V> o1, Entry<K, V> o2) {
                return o1.getValue().compareTo(o2.getValue());
            }
        });

        // LinkedHashMap will keep the keys in the order they are inserted
        // which is currently sorted on natural ordering
        Map<K, V> sortedMap = new LinkedHashMap<K, V>();

        for (Map.Entry<K, V> entry : entries) {
            sortedMap.put(entry.getKey(), entry.getValue());
        }

        return sortedMap;
    }

    public class CustomAdapter extends BaseAdapter {
        public CustomAdapter() {

        }

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return sorted.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.BaseAdapter#getViewTypeCount()
         */
        @Override
        public int getViewTypeCount() {
            // TODO Auto-generated method stub
            return sorted.size();
        }

        /*
         * (non-Javadoc)
         * 
         * @see android.widget.BaseAdapter#getItemViewType(int)
         */
        @Override
        public int getItemViewType(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            if (convertView == null) {
                convertView = LI.inflate(R.layout.sort_raw, null);

                TextView txtname = (TextView) convertView
                        .findViewById(R.id.textView1);
                if (i.hasNext()) {
                    Map.Entry me = (Map.Entry) i.next();
                    System.out.print(me.getKey() + ": ");
                    System.out.println(me.getValue());
                    txtname.setText(me.getKey() + "  " + me.getValue());
                }
                //

            }
            return convertView;
        }
    }
}