Text FormattingAlphabetizing a phone number list

Information and discussion about LaTeX's general text formatting features (e.g. bold, italic, enumerations, ...)
Post Reply
thedoctor818
Posts: 92
Joined: Fri Apr 24, 2009 8:02 pm

Alphabetizing a phone number list

Post by thedoctor818 »

Hullo all. I am trying to alphabetise a list of phone numbers that I have in LaTeX. I was using the following method but the result is not good. Below is a sample of my code. Any help will be appreciated.

Code: Select all

\documentclass[a4paper,12pt]{report}
\usepackage{datatool}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{color}
\usepackage{amsmath, amssymb}
\usepackage[left=1.00cm, right=0.85cm, top=1.25cm, bottom=1.50cm]{geometry}

\newcommand{\sortitem}[2]{%
  \DTLnewrow{list}%
  \DTLnewdbentry{list}{label}{#1}%
  \DTLnewdbentry{list}{description}{#2}%
}

\newenvironment{sortedlist}%
{%
  \DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}%
}%
{%
  \DTLsort{label}{list}%
  \begin{description}%
    \DTLforeach*{list}{\theLabel=label,\theDesc=description}{%
      \item[\theLabel] \theDesc
    }%
  \end{description}%
}

\title{Phone List.}
\author{The Doctor.}

\begin{document}

\chapter*{Alphanumeric phone listing.}
\begin{sortedlist}
  \sortitem Rob Clark--404.025.1851
  \sortitem Xie Xhouyan--478.387.5902
  \sortitem Helni E Roberlo--678.310.4134
  \sortitem Daniel Moss--912.695.5738
  \sortitem David Brown--702.327.0403
\end{sortedlist}

\end{document}
Thanks in advance.
Last edited by thedoctor818 on Wed Jan 26, 2011 12:35 am, edited 1 time in total.
-Michael D

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

NEW: TikZ book now 40% off at Amazon.com for a short time.

And: Currently, Packt sells ebooks for $4.99 each if you buy 5 of their over 1000 ebooks. If you choose only a single one, $9.99. How about combining 3 LaTeX books with Python, gnuplot, mathplotlib, Matlab, ChatGPT or other AI books? Epub and PDF. Bundle (3 books, add more for higher discount): https://packt.link/MDH5p

frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Alphabetizing a phone number list

Post by frabjous »

You seem to have taken your code for a sorted list from this thread here. As should be clear there, the \sortitem command is a command that takes two arguments, and must be written \sortitem{...}{...}, where the first argument is filled with the sort term, and additional information in the second argument.

So you could try:

Code: Select all

\documentclass[a4paper,12pt]{report}
\usepackage{datatool}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{color}
\usepackage{amsmath, amssymb}
\usepackage[left=1.00cm, right=0.85cm, top=1.25cm, bottom=1.50cm]{geometry}

\newcommand{\sortitem}[2]{%
  \DTLnewrow{list}%
  \DTLnewdbentry{list}{label}{#1}%
  \DTLnewdbentry{list}{description}{#2}%
}

\newenvironment{sortedlist}%
{%
  \DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}%
}%
{%
  \DTLsort{label}{list}%
  \begin{description}%
    \DTLforeach*{list}{\theLabel=label,\theDesc=description}{%
      \item[\theLabel] \theDesc
    }%
  \end{description}%
}

\title{Phone List.}
\author{The Doctor.}

\begin{document}

\chapter*{Alphanumeric phone listing.}
\begin{sortedlist}
  \sortitem{Rob Clark}{404.025.1851}
  \sortitem{Xie Xhouya}{478.387.5902}
  \sortitem{Helni E Roberlo}{678.310.4134}
  \sortitem{Daniel Moss}{912.695.5738}
  \sortitem{David Brown}{702.327.0403}
\end{sortedlist}

\end{document}
Of course, that sorts by first name rather than last name, which is perhaps not what you want, but it's really unclear what you do want.
thedoctor818
Posts: 92
Joined: Fri Apr 24, 2009 8:02 pm

Re: Alphabetizing a phone number list

Post by thedoctor818 »

Thanks, that is what I was looking for. But, out of curiosity, how would you sort by last name? Thanks again.
-Michael D
User avatar
frabjous
Posts: 2064
Joined: Fri Mar 06, 2009 12:20 am

Alphabetizing a phone number list

Post by frabjous »

If you wanted to sort by lastname, you could do something like this. Notice, I changed \sortitem to have three arguments rather than two.

Code: Select all

\documentclass[a4paper,12pt]{report}
\usepackage{datatool}
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{lmodern}
\usepackage{color}
\usepackage{amsmath, amssymb}
\usepackage[left=1.00cm, right=0.85cm, top=1.25cm, bottom=1.50cm]{geometry}

\newcommand{\sortitem}[3]{%
  \DTLnewrow{list}%
  \DTLnewdbentry{list}{firstname}{#1}%
  \DTLnewdbentry{list}{lastname}{#2}%
  \DTLnewdbentry{list}{phonenumber}{#3}%
}

\newenvironment{sortedlist}%
{%
  \DTLifdbexists{list}{\DTLcleardb{list}}{\DTLnewdb{list}}%
}%
{%
  \DTLsort{lastname}{list}%
  \begin{description}%
    \DTLforeach*{list}{\theFirstname=firstname,\theLastname=lastname,\thePhonenumber=phonenumber}{%
      \item[\theFirstname~\theLastname] \thePhonenumber
    }%
  \end{description}%
}

\title{Phone List.}
\author{The Doctor.}

\begin{document}

\chapter*{Alphanumeric phone listing.}
\begin{sortedlist}
  \sortitem{Rob}{Clark}{404.025.1851}
  \sortitem{Xie}{Xhouya}{478.387.5902}
  \sortitem{Helni E}{Roberlo}{678.310.4134}
  \sortitem{Daniel}{Moss}{912.695.5738}
  \sortitem{David}{Brown}{702.327.0403}
\end{sortedlist}

\end{document}
That still lists the first name first. If you wanted it as Lastname, firstname, then you'd need to change the line:

Code: Select all

      \item[\theFirstname~\theLastname] \thePhonenumber
to

Code: Select all

      \item[\theLastname,~\theFirstname] \thePhonenumber
By the way, I hope those aren't real people's real phone numbers you're putting online!
Post Reply