用数字排序数组(VB.NET)

时间:2014-04-15 19:04:17

标签: arrays vb.net sorting csv

我想根据整数值按降序对记录进行排序:

示例:

name1, 4
name2, 6
name3, 3
name4, 5

应该重新安排到这个:

name2, 6
name4, 5
name1, 4
name3, 3

我尝试过使用Array.Sort,但我无法使用它。

我一如既往地感谢您的帮助。

3 个答案:

答案 0 :(得分:2)

您可以将数据拆分为两个数组,并使用array.sort根据整数进行排序。

Dim a() As String = {"name1", "name2", "name3", "name4"}
Dim ia() As Integer = {4, 6, 3, 5}
Array.Sort(ia, a)

这将按ia的升序对两个数组进行排序。向后迭代数组以降序。

答案 1 :(得分:0)

Sub Main()
    Dim StartArray(3) As Integer
'First let's assign the array elements before it is sorted
        StartArray(0) = 4
        StartArray(1) = 6
        StartArray(2) = 3
        StartArray(3) = 5
        Array.Sort(StartArray) 'This sorts the array
        For i As Integer = 0 To 3
            Console.WriteLine(StartArray(i)) 'Prints the array elements to console
        Next
        Console.ReadLine()
End Sub

答案 2 :(得分:0)

package popUpTest;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class PopUpTest
    {

        private JFrame frame;
        private JPasswordField passSourcePassword;
        private JTextField txtSourceUserName;
        private JTextField txtTargetUserName;
        private JPasswordField passTargetPassword;

        /**
         * Launch the application.
         */
        public static void main(String[] args)
            {
                EventQueue.invokeLater(new Runnable()
                    {
                        public void run()
                            {
                                try
                                    {
                                        PopUpTest window = new PopUpTest();
                                        window.frame.setVisible(true);
                                    } catch (Exception e)
                                    {
                                        e.printStackTrace();
                                    }
                            }
                    });
            }

        /**
         * Create the application.
         */
        public PopUpTest()
            {
                initialize();
            }

        /**
         * Initialize the contents of the frame.
         */
        private void initialize()
            {
                frame = new JFrame();
                frame.setBounds(100, 100, 444, 403);
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.getContentPane().setLayout(null);

                JLabel lblSourceUserName = new JLabel("SourceUserName:");
                lblSourceUserName.setFont(new Font("Tahoma", Font.BOLD, 16));
                lblSourceUserName.setBounds(15, 62, 170, 20);
                frame.getContentPane().add(lblSourceUserName);

                passSourcePassword = new JPasswordField();
                passSourcePassword.setBounds(15, 153, 147, 26);
                frame.getContentPane().add(passSourcePassword);

                JLabel lblSourcePassword = new JLabel("SourcePassword:");
                lblSourcePassword.setFont(new Font("Tahoma", Font.BOLD, 16));
                lblSourcePassword.setBounds(15, 132, 147, 20);
                frame.getContentPane().add(lblSourcePassword);

                txtSourceUserName = new JTextField();
                txtSourceUserName.setBounds(16, 81, 146, 26);
                frame.getContentPane().add(txtSourceUserName);
                txtSourceUserName.setColumns(10);

                JLabel lblTargetUserName = new JLabel("TargetUserName:");
                lblTargetUserName.setFont(new Font("Tahoma", Font.BOLD, 16));
                lblTargetUserName.setBounds(240, 62, 170, 20);
                frame.getContentPane().add(lblTargetUserName);

                txtTargetUserName = new JTextField();
                txtTargetUserName.setColumns(10);
                txtTargetUserName.setBounds(241, 81, 146, 26);
                frame.getContentPane().add(txtTargetUserName);

                JLabel lblTargetPassword = new JLabel("TargetPassword:");
                lblTargetPassword.setFont(new Font("Tahoma", Font.BOLD, 16));
                lblTargetPassword.setBounds(240, 132, 147, 20);
                frame.getContentPane().add(lblTargetPassword);

                passTargetPassword = new JPasswordField();
                passTargetPassword.setBounds(240, 153, 147, 26);
                frame.getContentPane().add(passTargetPassword);

                JLabel lblEnterBelowDetails = new JLabel("Enter Below Details:");
                lblEnterBelowDetails.setFont(new Font("Tahoma", Font.BOLD, 16));
                lblEnterBelowDetails.setBounds(15, 26, 187, 20);
                frame.getContentPane().add(lblEnterBelowDetails);

                JTextArea textArea = new JTextArea();
                textArea.setBounds(15, 252, 365, 85);
                frame.getContentPane().add(textArea);

                JButton btnShowAllDetails = new JButton("Show All Details:");
                btnShowAllDetails.setBounds(15, 207, 226, 29);
                frame.getContentPane().add(btnShowAllDetails);

                btnShowAllDetails.addActionListener(new ActionListener()
                    {
                        public void actionPerformed(ActionEvent arg0)
                            {
                                textArea.append(txtSourceUserName.getText() + "\n");
                                textArea.append(passSourcePassword.getText() + "\n");
                                textArea.append(txtTargetUserName.getText() + "\n");
                                textArea.append(passTargetPassword.getText() + "\n");
                            }

                    });

            }
    }