<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="ru">
		<id>http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FSWT%2FTable_Sort</id>
		<title>Java Tutorial/SWT/Table Sort - История изменений</title>
		<link rel="self" type="application/atom+xml" href="http://www.jexp.ru/index.php?action=history&amp;feed=atom&amp;title=Java_Tutorial%2FSWT%2FTable_Sort"/>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/SWT/Table_Sort&amp;action=history"/>
		<updated>2026-04-21T14:15:58Z</updated>
		<subtitle>История изменений этой страницы в вики</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/SWT/Table_Sort&amp;diff=2991&amp;oldid=prev</id>
		<title> в 17:44, 31 мая 2010</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/SWT/Table_Sort&amp;diff=2991&amp;oldid=prev"/>
				<updated>2010-05-31T17:44:26Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;table class=&quot;diff diff-contentalign-left&quot; data-mw=&quot;interface&quot;&gt;
				&lt;tr style=&quot;vertical-align: top;&quot; lang=&quot;ru&quot;&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;← Предыдущая&lt;/td&gt;
				&lt;td colspan=&quot;1&quot; style=&quot;background-color: white; color:black; text-align: center;&quot;&gt;Версия 17:44, 31 мая 2010&lt;/td&gt;
				&lt;/tr&gt;&lt;tr&gt;&lt;td colspan=&quot;2&quot; style=&quot;text-align: center;&quot; lang=&quot;ru&quot;&gt;&lt;div class=&quot;mw-diff-empty&quot;&gt;(нет различий)&lt;/div&gt;
&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;</summary>
			</entry>

	<entry>
		<id>http://www.jexp.ru/index.php?title=Java_Tutorial/SWT/Table_Sort&amp;diff=2992&amp;oldid=prev</id>
		<title>Admin: 1 версия</title>
		<link rel="alternate" type="text/html" href="http://www.jexp.ru/index.php?title=Java_Tutorial/SWT/Table_Sort&amp;diff=2992&amp;oldid=prev"/>
				<updated>2010-05-31T15:19:44Z</updated>
		
		<summary type="html">&lt;p&gt;1 версия&lt;/p&gt;
&lt;p&gt;&lt;b&gt;Новая страница&lt;/b&gt;&lt;/p&gt;&lt;div&gt;==  Sort a table by column ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*******************************************************************************&lt;br /&gt;
 * Copyright (c) 2000, 2004 IBM Corporation and others.&lt;br /&gt;
 * All rights reserved. This program and the accompanying materials&lt;br /&gt;
 * are made available under the terms of the Eclipse Public License v1.0&lt;br /&gt;
 * which accompanies this distribution, and is available at&lt;br /&gt;
 * http://www.eclipse.org/legal/epl-v10.html&lt;br /&gt;
 *&lt;br /&gt;
 * Contributors:&lt;br /&gt;
 *     IBM Corporation - initial API and implementation&lt;br /&gt;
 *******************************************************************************/&lt;br /&gt;
//package org.eclipse.swt.snippets;&lt;br /&gt;
/*&lt;br /&gt;
 * Table example snippet: sort a table by column&lt;br /&gt;
 * &lt;br /&gt;
 * For a list of all SWT example snippets see&lt;br /&gt;
 * http://www.eclipse.org/swt/snippets/&lt;br /&gt;
 * &lt;br /&gt;
 * @since 3.2&lt;br /&gt;
 */&lt;br /&gt;
import java.text.Collator;&lt;br /&gt;
import java.util.Locale;&lt;br /&gt;
import org.eclipse.swt.SWT;&lt;br /&gt;
import org.eclipse.swt.layout.FillLayout;&lt;br /&gt;
import org.eclipse.swt.widgets.Display;&lt;br /&gt;
import org.eclipse.swt.widgets.Event;&lt;br /&gt;
import org.eclipse.swt.widgets.Listener;&lt;br /&gt;
import org.eclipse.swt.widgets.Shell;&lt;br /&gt;
import org.eclipse.swt.widgets.Table;&lt;br /&gt;
import org.eclipse.swt.widgets.TableColumn;&lt;br /&gt;
import org.eclipse.swt.widgets.TableItem;&lt;br /&gt;
public class TableSortByColumn {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    Display display = new Display();&lt;br /&gt;
    Shell shell = new Shell(display);&lt;br /&gt;
    shell.setLayout(new FillLayout());&lt;br /&gt;
    final Table table = new Table(shell, SWT.BORDER);&lt;br /&gt;
    table.setHeaderVisible(true);&lt;br /&gt;
    final TableColumn column1 = new TableColumn(table, SWT.NONE);&lt;br /&gt;
    column1.setText(&amp;quot;Column 1&amp;quot;);&lt;br /&gt;
    final TableColumn column2 = new TableColumn(table, SWT.NONE);&lt;br /&gt;
    column2.setText(&amp;quot;Column 2&amp;quot;);&lt;br /&gt;
    TableItem item = new TableItem(table, SWT.NONE);&lt;br /&gt;
    item.setText(new String[] { &amp;quot;a&amp;quot;, &amp;quot;3&amp;quot; });&lt;br /&gt;
    item = new TableItem(table, SWT.NONE);&lt;br /&gt;
    item.setText(new String[] { &amp;quot;b&amp;quot;, &amp;quot;2&amp;quot; });&lt;br /&gt;
    item = new TableItem(table, SWT.NONE);&lt;br /&gt;
    item.setText(new String[] { &amp;quot;c&amp;quot;, &amp;quot;1&amp;quot; });&lt;br /&gt;
    column1.setWidth(100);&lt;br /&gt;
    column2.setWidth(100);&lt;br /&gt;
    Listener sortListener = new Listener() {&lt;br /&gt;
      public void handleEvent(Event e) {&lt;br /&gt;
        TableItem[] items = table.getItems();&lt;br /&gt;
        Collator collator = Collator.getInstance(Locale.getDefault());&lt;br /&gt;
        TableColumn column = (TableColumn) e.widget;&lt;br /&gt;
        int index = column == column1 ? 0 : 1;&lt;br /&gt;
        for (int i = 1; i &amp;lt; items.length; i++) {&lt;br /&gt;
          String value1 = items[i].getText(index);&lt;br /&gt;
          for (int j = 0; j &amp;lt; i; j++) {&lt;br /&gt;
            String value2 = items[j].getText(index);&lt;br /&gt;
            if (collator.rupare(value1, value2) &amp;lt; 0) {&lt;br /&gt;
              String[] values = { items[i].getText(0), items[i].getText(1) };&lt;br /&gt;
              items[i].dispose();&lt;br /&gt;
              TableItem item = new TableItem(table, SWT.NONE, j);&lt;br /&gt;
              item.setText(values);&lt;br /&gt;
              items = table.getItems();&lt;br /&gt;
              break;&lt;br /&gt;
            }&lt;br /&gt;
          }&lt;br /&gt;
        }&lt;br /&gt;
        table.setSortColumn(column);&lt;br /&gt;
      }&lt;br /&gt;
    };&lt;br /&gt;
    column1.addListener(SWT.Selection, sortListener);&lt;br /&gt;
    column2.addListener(SWT.Selection, sortListener);&lt;br /&gt;
    table.setSortColumn(column1);&lt;br /&gt;
    table.setSortDirection(SWT.UP);&lt;br /&gt;
    shell.setSize(shell.ruputeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);&lt;br /&gt;
    shell.open();&lt;br /&gt;
    while (!shell.isDisposed()) {&lt;br /&gt;
      if (!display.readAndDispatch())&lt;br /&gt;
        display.sleep();&lt;br /&gt;
    }&lt;br /&gt;
    display.dispose();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
==  Sort table column in two direction with custom Comparator ==&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- start source code --&amp;gt;&lt;br /&gt;
   &lt;br /&gt;
    &amp;lt;source lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
/*******************************************************************************&lt;br /&gt;
 * Copyright (c) 2000, 2005 IBM Corporation and others. All rights reserved.&lt;br /&gt;
 * This program and the accompanying materials are made available under the&lt;br /&gt;
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,&lt;br /&gt;
 * and is available at http://www.eclipse.org/legal/epl-v10.html&lt;br /&gt;
 * &lt;br /&gt;
 * Contributors: IBM Corporation - initial API and implementation&lt;br /&gt;
 ******************************************************************************/&lt;br /&gt;
// package org.eclipse.swt.snippets;&lt;br /&gt;
import java.util.Arrays;&lt;br /&gt;
import java.util.ruparator;&lt;br /&gt;
import java.util.Random;&lt;br /&gt;
import org.eclipse.swt.SWT;&lt;br /&gt;
import org.eclipse.swt.layout.FillLayout;&lt;br /&gt;
import org.eclipse.swt.widgets.Display;&lt;br /&gt;
import org.eclipse.swt.widgets.Event;&lt;br /&gt;
import org.eclipse.swt.widgets.Listener;&lt;br /&gt;
import org.eclipse.swt.widgets.Shell;&lt;br /&gt;
import org.eclipse.swt.widgets.Table;&lt;br /&gt;
import org.eclipse.swt.widgets.TableColumn;&lt;br /&gt;
import org.eclipse.swt.widgets.TableItem;&lt;br /&gt;
/*&lt;br /&gt;
 * Show a sort indicator in the column header&lt;br /&gt;
 * &lt;br /&gt;
 * For a list of all SWT example snippets see&lt;br /&gt;
 * http://www.eclipse.org/swt/snippets/&lt;br /&gt;
 * &lt;br /&gt;
 * @since 3.2&lt;br /&gt;
 */&lt;br /&gt;
public class TableSortTwoDirection {&lt;br /&gt;
  public static void main(String[] args) {&lt;br /&gt;
    // initialize data with keys and random values&lt;br /&gt;
    int size = 100;&lt;br /&gt;
    Random random = new Random();&lt;br /&gt;
    final int[][] data = new int[size][];&lt;br /&gt;
    for (int i = 0; i &amp;lt; data.length; i++) {&lt;br /&gt;
      data[i] = new int[] { i, random.nextInt() };&lt;br /&gt;
    }&lt;br /&gt;
    // create a virtual table to display data&lt;br /&gt;
    Display display = new Display();&lt;br /&gt;
    Shell shell = new Shell(display);&lt;br /&gt;
    shell.setLayout(new FillLayout());&lt;br /&gt;
    final Table table = new Table(shell, SWT.VIRTUAL);&lt;br /&gt;
    table.setHeaderVisible(true);&lt;br /&gt;
    table.setLinesVisible(true);&lt;br /&gt;
    table.setItemCount(size);&lt;br /&gt;
    final TableColumn column1 = new TableColumn(table, SWT.NONE);&lt;br /&gt;
    column1.setText(&amp;quot;Key&amp;quot;);&lt;br /&gt;
    column1.setWidth(200);&lt;br /&gt;
    final TableColumn column2 = new TableColumn(table, SWT.NONE);&lt;br /&gt;
    column2.setText(&amp;quot;Value&amp;quot;);&lt;br /&gt;
    column2.setWidth(200);&lt;br /&gt;
    table.addListener(SWT.SetData, new Listener() {&lt;br /&gt;
      public void handleEvent(Event e) {&lt;br /&gt;
        TableItem item = (TableItem) e.item;&lt;br /&gt;
        int index = table.indexOf(item);&lt;br /&gt;
        int[] datum = data[index];&lt;br /&gt;
        item.setText(new String[] { Integer.toString(datum[0]), Integer.toString(datum[1]) });&lt;br /&gt;
      }&lt;br /&gt;
    });&lt;br /&gt;
    // Add sort indicator and sort data when column selected&lt;br /&gt;
    Listener sortListener = new Listener() {&lt;br /&gt;
      public void handleEvent(Event e) {&lt;br /&gt;
        // determine new sort column and direction&lt;br /&gt;
        TableColumn sortColumn = table.getSortColumn();&lt;br /&gt;
        TableColumn currentColumn = (TableColumn) e.widget;&lt;br /&gt;
        int dir = table.getSortDirection();&lt;br /&gt;
        if (sortColumn == currentColumn) {&lt;br /&gt;
          dir = dir == SWT.UP ? SWT.DOWN : SWT.UP;&lt;br /&gt;
        } else {&lt;br /&gt;
          table.setSortColumn(currentColumn);&lt;br /&gt;
          dir = SWT.UP;&lt;br /&gt;
        }&lt;br /&gt;
        // sort the data based on column and direction&lt;br /&gt;
        final int index = currentColumn == column1 ? 0 : 1;&lt;br /&gt;
        final int direction = dir;&lt;br /&gt;
        Arrays.sort(data, new Comparator() {&lt;br /&gt;
          public int compare(Object arg0, Object arg1) {&lt;br /&gt;
            int[] a = (int[]) arg0;&lt;br /&gt;
            int[] b = (int[]) arg1;&lt;br /&gt;
            if (a[index] == b[index])&lt;br /&gt;
              return 0;&lt;br /&gt;
            if (direction == SWT.UP) {&lt;br /&gt;
              return a[index] &amp;lt; b[index] ? -1 : 1;&lt;br /&gt;
            }&lt;br /&gt;
            return a[index] &amp;lt; b[index] ? 1 : -1;&lt;br /&gt;
          }&lt;br /&gt;
        });&lt;br /&gt;
        // update data displayed in table&lt;br /&gt;
        table.setSortDirection(dir);&lt;br /&gt;
        table.clearAll();&lt;br /&gt;
      }&lt;br /&gt;
    };&lt;br /&gt;
    column1.addListener(SWT.Selection, sortListener);&lt;br /&gt;
    column2.addListener(SWT.Selection, sortListener);&lt;br /&gt;
    table.setSortColumn(column1);&lt;br /&gt;
    table.setSortDirection(SWT.UP);&lt;br /&gt;
    shell.setSize(shell.ruputeSize(SWT.DEFAULT, SWT.DEFAULT).x, 300);&lt;br /&gt;
    shell.open();&lt;br /&gt;
    while (!shell.isDisposed()) {&lt;br /&gt;
      if (!display.readAndDispatch())&lt;br /&gt;
        display.sleep();&lt;br /&gt;
    }&lt;br /&gt;
    display.dispose();&lt;br /&gt;
  }&lt;br /&gt;
}&amp;lt;/source&amp;gt;&lt;br /&gt;
    &lt;br /&gt;
   &lt;br /&gt;
  &amp;lt;!-- end source code --&amp;gt;&lt;/div&gt;</summary>
		<author><name>Admin</name></author>	</entry>

	</feed>